Build fluency in the vocabulary of a lock where a waiting thread busy-waits in a loop instead of sleeping.
0 / 5 completed
1 / 5
At standup, a dev mentions a lock where a thread waiting to acquire it sits in a tight loop repeatedly checking whether the lock is free, instead of being put to sleep and woken up later by the operating system. What is this kind of lock called?
A spinlock is exactly this: a lock where a waiting thread sits in a tight loop repeatedly checking whether the lock has become free, rather than yielding the CPU and being put to sleep by the operating system until it's woken up later. A deadlock is an unrelated concurrency failure about threads permanently blocked on each other, not about how a single thread waits for one lock. This busy-wait behavior is exactly why a spinlock avoids the cost of a context switch, at the price of burning CPU cycles the whole time it waits.
2 / 5
During a design review, the team picks a spinlock specifically for a critical section that's held only for an extremely short time, rather than using a lock that puts a waiting thread to sleep. Which capability does the spinlock provide here?
A spinlock here provides avoiding the cost of a context switch for a wait that's expected to be shorter than that switch would even take, since busy-waiting briefly in a loop can be cheaper overall than paying the operating system's cost of putting a thread to sleep and later waking it back up, when the actual wait is extremely short. A sleeping lock instead pays that context-switch cost on every wait, which is wasteful when the critical section is held only briefly. This short-hold assumption is exactly why a spinlock is reserved for critical sections expected to be held for a very short, bounded amount of time.
3 / 5
In a code review, a dev notices a spinlock protecting a critical section that occasionally performs a slow network call while holding the lock, meaning other threads can end up busy-waiting for a long, unpredictable time. What does this represent?
This is a misuse of a spinlock, since busy-waiting is only cheap when the wait is expected to be short, and a critical section that occasionally makes a slow network call can hold the lock for a long, unpredictable time, during which every other waiting thread burns CPU cycles doing nothing productive. A cache eviction policy is an unrelated concept about discarded cache entries. This is exactly the kind of pattern a reviewer flags, since a sleeping lock would let waiting threads yield the CPU instead of wasting it during an unpredictably long hold.
4 / 5
An incident report shows CPU usage spiked sharply under load because a spinlock was protecting a critical section that occasionally performed a slow network call, leaving many threads busy-waiting and burning CPU cycles for an unpredictably long time. What practice would prevent this?
Replacing the spinlock with a sleeping lock for this critical section lets a waiting thread yield the CPU and be woken up later instead of busy-waiting the whole time, which directly eliminates the wasted CPU cycles that spiked usage in this incident. Continuing to use the spinlock regardless of the network call's unpredictable duration is exactly what let so many threads burn CPU busy-waiting under load. This is the standard fix once a critical section's hold time is no longer reliably short, since that's precisely the assumption a spinlock's cheap busy-wait depends on.
5 / 5
During a PR review, a teammate asks why the team reserves spinlocks for extremely short critical sections instead of using them everywhere a lock is needed, given that a spinlock avoids the overhead of a sleeping lock's context switch. What is the reasoning?
A spinlock's busy-waiting thread burns real CPU cycles for the entire time it waits, so once a critical section's hold time becomes longer or less predictable, that wasted CPU cost can easily outweigh the context-switch savings the spinlock was chosen for in the first place. A sleeping lock instead lets a waiting thread give up the CPU entirely, which costs a context switch but avoids burning cycles unproductively during a longer wait. The tradeoff is exactly why spinlocks are reserved for critical sections with a short, reliably bounded hold time, and sleeping locks are used everywhere a wait might stretch on unpredictably.