Two-Phase Locks
This lesson discusses two-phase locks, a rather older approach.
We'll cover the following
One final note
The Linux approach has the flavor of an old approach that has been used on and off for years, going at least as far back to
However, if the lock is not acquired during the first spin phase, a second phase is entered, where the caller is put to sleep, and only woken up when the lock becomes free later. The Linux lock above is a form of such a lock, but it only spins once; a generalization of this could spin in a loop for a fixed amount of time before using futex support to sleep.
Two-phase locks are yet another instance of a hybrid approach, where combining two good ideas may indeed yield a better one. Of course, whether it does depends strongly on many things, including the hardware environment, the number of threads, and other workload details. As always, making a single general-purpose lock, good for all possible use cases, is quite a challenge.
Get hands-on with 1400+ tech skills courses.