r/programming • • Sep 09 '26

Optimizing a Spin-Lock

https://david.alvarezrosa.com/posts/optimizing-a-spin-lock/
183 Upvotes

32 comments sorted by

View all comments

Show parent comment

2

u/TwoWeeks90DaysTops Sep 10 '26

I fixed a slow shutdown (so that Windows would claim that the service didn't respond in time) in a Windows service written in C++ a decade or so ago, and it was caused by the fact that the code hand-rolled its own spinlocks with `volatile bool`. The issue was as I remember it two part: the spinlock consumes resources on contention, and the `volatile bool` thing is about instruction ordering, and not that suitable for locking. I replaced them with mutexes and the application shutdown was almost immediate.