Busy Waiting and Spinlocks
Busy waiting is when a process repeatedly checks a condition in a tight loop instead of yielding the CPU while it waits, burning CPU cycles without doing useful work. A spinlock is a lock built directly on busy waiting: a process spins until the lock is free, which is efficient for very short waits but wasteful for long ones.
When a process needs to wait for a condition to become true before it can proceed, there are two fundamentally different ways to wait: give up the CPU entirely and let the OS wake you later, or keep the CPU and just keep checking the condition over and over in a tight loop. That second approach is called busy waiting, and a spinlock is a real, practical synchronization primitive built directly on it.
What busy waiting actually is
Peterson's Solution's while (flag[j] && turn == j) { } loop is a real example of busy waiting: the process doesn't release the CPU while it waits, it just keeps re-checking the condition as fast as it possibly can, burning CPU cycles the entire time it's waiting, doing no useful work.
What a spinlock is
A spinlock is a lock implemented using exactly this busy-waiting idea: to acquire it, a process repeatedly checks (in a tight loop, "spinning") whether the lock is free, and the instant it becomes free, grabs it atomically (using a hardware-supported atomic instruction like test-and-set, so no race condition can occur on the lock's own state). To release it, the holding process just sets the lock's state back to free, letting whichever spinning process notices first grab it.
The real tradeoff
Busy waiting has one genuine cost: it wastes CPU cycles the entire time it's spinning, cycles that could otherwise go to a different, genuinely useful process. But it also has one genuine benefit: it has essentially zero overhead to actually wake up once the condition becomes true, the spinning process notices almost instantly, since it's continuously checking, with none of the delay involved in the OS suspending a process and later rescheduling it back onto a CPU.
This makes spinlocks a real net win specifically when the expected wait is very short, shorter than the overhead of a full context switch to sleep and later wake the waiting process. If the wait is expected to be long, a normal semaphore-style lock (where the waiting process actually blocks and releases the CPU entirely) is far more efficient overall, since the wasted spinning cycles would badly outweigh any wakeup-speed benefit.
A concrete example
Picture a multi-core system where one CPU core is about to release a lock that's held for only a few dozen CPU instructions, an extremely brief critical section. A different core waiting on that same lock via a spinlock will likely notice it's free and grab it within a handful of spins, faster than the OS could have even finished the bookkeeping to put the waiting process to sleep and later wake it back up. Now compare that to a lock protecting a critical section that does a slow disk read, potentially milliseconds: a spinning waiter here would burn an enormous number of wasted CPU cycles for that entire disk-read duration, while a properly blocking wait would let that CPU core do other useful work during the wait instead.
Where this matters in real systems
Operating system kernels use spinlocks internally specifically because they often need to protect extremely short critical sections (a handful of instructions manipulating a small piece of kernel data), where the overhead of a real context switch to sleep and wake would dwarf the actual wait itself, this is exactly why Linux's kernel makes real, extensive use of spinlocks internally, not as a mistake, but as a deliberate, well-reasoned choice for very short critical sections specifically on multi-core systems (spinning makes no sense at all on a single-core system, since the lock holder can't make progress to release it while the only CPU is busy spinning instead of running it).
What interviewers actually ask about busy waiting and spinlocks
- "What is busy waiting?" Repeatedly checking a condition in a tight loop while holding onto the CPU, instead of yielding it and being woken later.
- "What is a spinlock, and how does it relate to busy waiting?" A lock implemented using busy waiting: a process trying to acquire it spins, repeatedly checking if it's free, and grabs it atomically the instant it is.
- "When is a spinlock actually a good choice, and when is it a bad one?" Good for very short expected waits, where spinning costs less than a full context switch would; bad for long waits, where the wasted spinning cycles badly outweigh any wakeup-speed benefit.
- "Why would a spinlock never make sense on a single-core system?" Because the lock holder can't run to release the lock while the only CPU core is busy spinning instead of actually executing the holder's code, the waiting process would spin forever with no way for the lock to ever be released.
Key takeaways
- Busy waiting means repeatedly checking a condition in a loop while holding the CPU, rather than yielding it.
- A spinlock is a lock built on busy waiting, efficient for very short waits, wasteful for long ones.
- The tradeoff is wasted CPU cycles while spinning versus near-instant reaction once the condition becomes true, with no context-switch overhead.
- Spinlocks make real sense in OS kernels protecting short critical sections on multi-core systems, but never make sense at all on a single-core system.
More in Process Synchronization
Browse all Operating Systems topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What is busy waiting?
Repeatedly checking a condition in a tight loop while holding onto the CPU, instead of yielding it and being woken later.
What is a spinlock, and how does it relate to busy waiting?
A lock implemented using busy waiting: a process trying to acquire it spins, repeatedly checking if it's free, and grabs it atomically the instant it is.
When is a spinlock actually a good choice, and when is it a bad one?
Good for very short expected waits, where spinning costs less than a full context switch would; bad for long waits, where the wasted spinning cycles badly outweigh any wakeup-speed benefit.
Why would a spinlock never make sense on a single-core system?
Because the lock holder can't run to release the lock while the only CPU core is busy spinning instead of actually executing the holder's code, the waiting process would spin forever with no way for the lock to ever be released.