Multicore & Synchronization

Which describes a spinlock?

  1. AA lock that puts the thread to sleep until the OS wakes it when the lock is free
  2. BA lock that waits by repeatedly checking the lock variable in a loop until it is free
  3. CA mechanism that protects data by spinning a disk
Show answer and explanation

Answer: B. A lock that waits by repeatedly checking the lock variable in a loop until it is free

A spinlock tries to acquire the lock with an atomic instruction and, if that fails, immediately tries again, over and over. There is no cost of calling the OS to sleep and wake, so it is fast when the lock is held only very briefly. It is common inside OS kernels and for mutual exclusion with interrupt handlers. Its drawback is that it keeps using the CPU while waiting.

  • A. Sleeping while waiting is what a mutex typically does.
  • B. Correct. It spins while waiting.
  • C. It has nothing to do with disks.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. In which situation is a spinlock least suitable?
  2. 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?
  3. Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?
  4. Which is an example of deadlock?
  5. In a program using several locks, what is the most common way to prevent deadlock?