Covering Conditions
Explore the concept of covering conditions in concurrency to understand how condition variables manage threads waiting for resources. This lesson examines a memory allocation example illustrating the problem of waking the correct thread. Learn why replacing signal calls with broadcast calls ensures proper thread synchronization, despite potential performance trade-offs.
We'll cover the following...
We’ll now look at one more example of how condition variables can be used. This code study is drawn from
The problem
The problem they ran into is best shown via simple example, in this case in a simple multi-threaded memory allocation library. Given below is a code snippet that demonstrates the issue.
As you might see in the code, when a thread calls into the memory allocation code, it might have to wait in order for more memory to become free. Conversely, when a thread frees memory, it signals that more memory is free. However, this code above has a problem: which waiting thread (there can be more than one) should be woken up?
Consider the following scenario. Assume there are zero bytes free; thread ...