Multicore & Synchronization

Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?

  1. AA mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work
  2. BA mutex keeps using the CPU while waiting, so it suits only short waits
  3. CThey are the same thing under different names
Show answer and explanation

Answer: A. A mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work

When a mutex cannot be acquired, the thread is put into a waiting state and the CPU goes to other threads; the OS wakes it when the lock is released. It wastes no CPU while waiting, so it suits long waits, but sleeping, waking and the context switch cost several microseconds. Real library mutexes combine both strengths, for example by spinning briefly before sleeping.

  • A. Correct. The choice depends on how long the wait is.
  • B. That describes a spinlock.
  • C. They differ in how they wait.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. Which is an example of deadlock?
  2. In a program using several locks, what is the most common way to prevent deadlock?
  3. What does a memory consistency model define?
  4. Which describes sequential consistency?
  5. The two threads above run simultaneously on different cores (assume no compiler reordering). Which outcome cannot occur under sequential consistency but can occur on real CPUs such as x86?