A Simple Approach: Just Yield, Baby
This lesson discusses the use of yield to avoid spinning.
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,
Get hands-on with 1400+ tech skills courses.