Multicore & Synchronization

What is the most suitable use of a counting semaphore?

  1. AAlways doubling the value of a variable
  2. BMerging all program execution into a single thread
  3. 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.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. What is the difference between cache coherence and a memory consistency model?
  2. Which describes the idea of hardware transactional memory?
  3. When waiting on a condition variable until a queue becomes non-empty, why is the condition rechecked in a while loop rather than an if after waking?
  4. Two cores each hold a copy of variable x in their own caches. After core 0 writes x, what problem could occur when core 1 reads x if there were no cache coherence mechanism?
  5. Which correctly lists the four states of the MESI cache coherence protocol?