Multicore & Synchronization
Which describes a spinlock?
- AA lock that puts the thread to sleep until the OS wakes it when the lock is free
- BA lock that waits by repeatedly checking the lock variable in a loop until it is free
- 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.