← Lessons

quiz vs the machine

Platinum1780

Concurrency

Spin Then Block Strategy

Briefly spinning before parking a thread to win on short waits without wasting cycles on long ones.

5 min read · advanced · beat Platinum to climb

Two bad extremes

Pure spinning burns the core while waiting, which is terrible for long waits. Pure blocking parks the thread immediately, which pays an expensive context switch even when the lock would free up in nanoseconds.

The hybrid

The spin then block strategy combines both.

  • On a failed acquire, the thread spins a bounded number of times, retrying the atomic check.
  • If the lock frees during the spin, it wins cheaply with no context switch.
  • If the spin budget runs out, the thread gives up and parks, releasing the core.

The spin window is tuned so that it usually covers the time a critical section holds the lock. Adaptive versions adjust the budget based on recent success.

Why it matters

Most real critical sections are very short. Spinning briefly converts a costly park and unpark into a few cheap retries, while the fallback to blocking keeps long waits from wasting the processor.

Key idea

Spin then block spins for a bounded window to catch short waits without a context switch, then parks the thread when the budget runs out, balancing low latency against wasted cycles.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem does pure blocking have on very short waits?

2. What does the thread do when the spin budget is exhausted?

3. Why is spin then block well suited to typical critical sections?