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?

  1. ABecause ordinary reads are more accurate than atomic instructions
  2. BBecause an atomic instruction may be used only once per program
  3. 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.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?
  2. Which is an example of deadlock?
  3. In a program using several locks, what is the most common way to prevent deadlock?
  4. What does a memory consistency model define?
  5. Which describes sequential consistency?