Spin locks are useful when enforcing critical sections that are themselves part of synchronization structures. Spin locks have all the same concurrency problems as mutexes, with the particular quirk that cyclic acquisition results not in deadlock, but in livelock . Atomicity from nothing. With care, it is in fact possible to create a spin lock that is atomic without assuming any interlocking at all, provided that interrupts only occur in between CPU instructions. Consider this .
Lets look at the pascal first to get the general idea.
We have an integer lock in memory. When trying to enter the lock, we first increment the lock in memory. We then read the value from memory into a local variable, and check, as before to see whether it is greater than zero. If it is, then someone else has the lock, and we go round again, otherwise, we have the lock. The important thing about this set of operations is that, given certain provisos, a thread switch can occur at any point in time, and this still remains thread safe.