Multicore & Synchronization
Why does a spinlock usually wait with ordinary reads and use an atomic instruction only when the lock looks free (test-and-test-and-set), instead of retrying the atomic write directly?
- ABecause ordinary reads are more accurate than atomic instructions
- BBecause an atomic instruction may be used only once per program
- CReads can wait inside the core's own cache, greatly reducing coherence traffic as the line bounces between cores
Show answer and explanation
Answer: C. Reads can wait inside the core's own cache, greatly reducing coherence traffic as the line bounces between cores
An atomic instruction involves a write, so waiting threads that keep executing it fight to own the line in the M state and keep invalidating others' copies. With ordinary reads, each core just keeps reading its Shared copy in its own cache, generating no traffic. Only when the lock is released and the value changes does a thread try to grab it with an atomic instruction.
- A. It is not about accuracy.
- B. There is no such limit.
- C. Correct. Waiters stop fighting over the line on every attempt.