Spinlocks vs. Mutexes: When to Spin and When to Sleep

Engineers feud: spin like a fidget spinner or let threads nap

TLDR: This piece explains when to busy‑wait (spin) and when to sleep with a lock, highlighting short waits favor hybrids and alignment tricks. Comments split between test‑loving pragmatists, hybrid‑mutex advocates, and lock‑free dreamers—plus jokes about turning servers into space heaters.

The internet just discovered that “spinlocks” are basically fidget spinners for servers—fast but noisy—while “mutexes” are naps with an alarm. [How Tech] breaks down the tradeoff: spinning wastes CPU, sleeping costs a wake‑up. But the real fireworks were in the comments, where engineers turned timing charts into a cage match. One camp cheered the practical demo, with staticfloat swooning over the built‑in test: “finally, receipts.” Another crowd fixated on cache drama: Tom1380 had a mini‑epiphany about padding data to avoid two locks sharing the same 64‑byte couch. Iconic.

Then came the spicy: charleslmunger dunked on the “just spin” advice, insisting a hybrid mutex—one that spins a tiny bit before sleeping—wins real‑world fights. Translation: don’t trust your stopwatch, it lies. Meanwhile, markisus kicked the hornet’s nest with “what about lock‑free?”—the mythical no‑lock path that often swaps bugs for brain-bending complexity. The meme of the day: “Perf top at 60%? Congrats, you built a space heater.” People joked about priority inversion (low‑priority thread holding the door while VIPs smash into it) and the infamous scheduler timeslice that turns spins into 100ms faceplants. Verdict? Use the right tool, measure ruthlessly, and please, align your locks—your CPUs are tired. Also, futex = sleep helper.

Key Points

  • Spinlocks busy-wait using atomic compare-and-swap, avoiding syscalls but consuming 100% CPU and causing cache line bouncing under contention.
  • Mutexes use a userspace fast path and, on contention, call futex to sleep, incurring syscall (~500 ns) and context switch (~3–5 μs) costs.
  • Uncontended mutex acquisition is fast (approximately 25–50 ns), so syscall overhead is only paid under contention.
  • Spinlocks are risky in preemptible userspace: if the lock holder is preempted, other threads can spin for an entire timeslice (e.g., ~100 ms on Linux).
  • Priority inversion and false sharing are major pitfalls; PI mutexes mitigate inversion, and aligning locks to cache lines (e.g., 64 bytes) reduces cache contention.

Hottest takes

"you can use a hybrid mutex and never actually park" — charleslmunger
"Where do lock free algorithms fall in this analysis?" — markisus
"I didn't know about using alignment to avoid cache bouncing. Fascinating stuff" — Tom1380
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.