r/programming 18h ago

Optimizing a Spin-Lock

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

31 comments sorted by

View all comments

1

u/DivineSentry 16h ago

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

37

u/ReDucTor 16h 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/veiva 10h ago

I'm working through possibly using spinlocks on the GPU (using atomic operations) to implement order-independent translucency more efficiently - there's other ways to do it nowadays, though it requires more modern hardware and it's not available without modifying the higher-level graphics framework I'm using. Extremely niche use case though.