We have talked about blocking locks, which voluntarily yield, and spinlocks, which just spin until they acquire the lock. At ?rst, it seems like spinlocks are verywasteful, and that one should always use blocking locks. But this question is a bit more complicated than that. In well-written code, locks are only held around small pieces of code, so they should not be held for very long. On a multi-processor system, it often makes sense to use spinlocks, since the thread holding the lock will likely release the lock soon, and may be running simultaneously on another processor, about to release the lock.
This question is further complicated, however, by the possibility of pagefaults (see virtual mem- ory, later in this course). Pagefaults or similar hard-to-predict delays couldmake evenwell-written multi-threaded code hold locks for longer than anticipated. A hybrid type of lock, called spin-then-yield locks, will spin for some amount of time, and if it still hasn't acquired the lock, yield. A spin-then-yield lock can be adaptive in the amount of time it spins before yielding.