A Simple Approach: Just Yield, Baby
Explore how yielding the CPU during lock contention can reduce wasted cycles in concurrency. Understand the mechanism of yield calls, their benefits for two-thread scenarios, and limitations when many threads contend. Learn why this approach still faces challenges such as starvation and context switch overhead.
We'll cover the following...
Hardware support got us pretty far: working locks, and even (as with the case of the ticket lock) fairness in lock acquisition. However, we still have a problem: what to do when a context switch occurs in a critical section, and threads start to spin endlessly, waiting for the interrupted (lock-holding) thread to be run again?
Lock with test-and-set and yield
Our first try is a simple and friendly approach: when you are going to spin, instead give up the CPU to another thread. As Al Davis might say, ...