Multicore & Synchronization
What is the most suitable use of a counting semaphore?
- AAlways doubling the value of a variable
- BMerging all program execution into a single thread
- CLetting up to four threads at a time use a resource with a fixed number of units, such as four connections
Show answer and explanation
Answer: C. Letting up to four threads at a time use a resource with a fixed number of units, such as four connections
A semaphore holds a counter of remaining units: a thread decrements it before use (waiting if it is 0) and increments it when done. With an initial value of 4, up to four threads can use the resource at once. With 1, it works like a lock for mutual exclusion. Waiting threads are usually put to sleep and woken by the OS.
- A. It does not compute values.
- B. It limits parallelism but does not merge everything into one thread.
- C. Correct. The counter tracks how many units remain.