Spinning around: Please don't – Common problems with spin locks

https://news.ycombinator.com/rss Hits: 5
Summary

Intro This is the 3rd project in less than a year where I’ve seen issues with spin-loops. I’ve been dealing with spinning threads for many years now, and I won’t lie: over the years I’ve been both on the offender and victim side. I’m getting tired of seeing the same issues again and again, which usually makes for a good reason to write a blog post so that, hopefully, people will read it and stop making the same mistakes others did. Actually, many others have written about this, covering various issues related to spin locks 1 2 3 4 5 6. But I guess there’s never enough material on those subjects. Some are about speed, others about fairness, a few about priority inversion, NUMA, and sometimes even about actually broken code. If this list hasn’t convinced you that things do spin out of control when using spin-locks, and that you should use OS primitives instead, keep reading. I’ll cover what you should not do when implementing your own spin-lock. Notice I said what you should NOT do, because, again, you should probably not use a spin-lock at all these days. And if you do… make sure you really, REALLY, REALLY know what you’re doing (spoiler: it will always come back to bite you when you least expect it). Note this is a story about spin loops in general, not about locking algorithms for which there are many 5. The broken spin-lock Let’s start with the basics, you want to implement your own spinlock. 🤪 “It’s easy! You simply have a boolean, a lock and an unlock function.” Right… For demonstration purposes, we are using int instead of bool as you might have something more complicated to do with it, such as storing metadata (for example: the thread ID). There are also quite a few pieces of code around that do not implement a spin-lock per se, but mutate some other content such as pointers. class BrokenSpinLock { // Using int32_t instead of bool on purpose, don't mind it. int32_t isLocked = 0; public: void lock() { while (isLocked != 0) // (1) { // Loop again until not locke...

First seen: 2026-01-28 19:28

Last seen: 2026-01-28 23:29