Better, But Still Broken: While, Not If
Understand how replacing if statements with while loops in condition variables prevents race conditions when synchronizing threads. Explore the problem of signaling with a single condition variable in producer-consumer scenarios and why signals must be directed to wake appropriate threads to avoid deadlocks.
We'll cover the following...
We'll cover the following...
In the last lesson, we saw how the solution we came up with for the producer/consumer problem did not work correctly for more than one consumer due to the race condition. Fortunately, fixing it is easy (see the code excerpt below): change the if to a while.
Why this works?
Think about why this ...