← Lessons

quiz vs the machine

Silver1100

Concurrency

Busy Waiting versus Blocking

Spinning on a condition versus sleeping until you are woken.

4 min read · intro · beat Silver to climb

Busy Waiting versus Blocking

When a thread must wait for a condition, it has two broad strategies. It can busy wait, also called spinning, by repeatedly checking the condition in a tight loop. Or it can block, handing the CPU back to the scheduler and going to sleep until something wakes it.

Busy waiting keeps the thread on the processor:

  • It burns CPU cycles doing nothing useful
  • It wastes energy and can starve other runnable threads
  • But it has very low latency because the thread reacts the instant the condition flips

Blocking removes the thread from the run queue:

  • The CPU is freed for other work
  • Waking the thread costs a context switch, adding latency
  • It scales far better when waits are long or many threads wait

The right choice depends on expected wait time. For extremely short waits, such as a lock held for a few instructions, spinning can win because a context switch costs more than the spin. This is why some locks use a brief spin and then fall back to blocking. For long or unpredictable waits, blocking is almost always correct.

A pure spin loop also needs a memory barrier or volatile read so the waiting thread actually sees updates from other threads.

Key idea

Busy waiting trades CPU for low latency on short waits, while blocking frees the CPU at the cost of a context switch and suits long waits.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the main drawback of busy waiting?

2. When can spinning outperform blocking?