Multicore & Synchronization
Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?
- AA mutex gives up the CPU while waiting, which suits long waits, but sleeping and waking cost OS work
- BA mutex keeps using the CPU while waiting, so it suits only short waits
- 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.