The spinlock variant
A bit of repetition here: we already understand how a mutex lock works normally; if a lock is already locked, then attempting to take the lock will cause that thread to block (wait upon) the unlock occurring. Let's dig a little deeper; how exactly do the loser threads block—wait upon — the unlock of the mutex? The answer is that, for the mutex lock, they do so by sleeping (being scheduled off CPU by the OS). This, in fact, is one of the defining properties of the mutex lock.
On the other hand, there exists a different kind of lock altogether—the spinlock (very commonly used within the Linux kernel) whose behavior is quite the opposite: it works by having the loser threads wait upon the unlock operation by spinning (polling)—well, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access