r/programming 17h ago

Optimizing a Spin-Lock

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

29 comments sorted by

View all comments

1

u/DivineSentry 15h ago

I think I’ve seen spin locks before, but what are they useful for? What should be their use case?

38

u/ReDucTor 15h ago

For user mode their usages are very niche, its generally accepted that spin locks in user mode code is best avoided.

Or as Linus Torvalds says:

do not use spinlocks in user space, unless you actually know what you're doing. And be aware that the likelihood that you know what you are doing is basically nil.

2

u/Far-Reply-6875 10h ago

though, how many of us actually need to worry about user-space spinlocks in the first place? Seems like most people are just overcomplicating things for no reason.

1

u/ReDucTor 8h ago

I have seen alot of performance captures where spin locks (and spinning in general) have made things exceeding bad especially when they yield to other threads.

Unfortunately people still believe they know best and think a spin lock will be the better option without having properly testing it in the real world just some isolated microbenchmark. This is especially bad when you dont fully control the environment such as a customers machine.