1/8
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a condition variable?
It is an explicit queue that threads can put themselves in and go to sleep when waiting for a condition to become desirable. Some other thread will wake all of the threads up whenever the variable changes its state.
Why is a condition variable more efficient than just using while loops to wait for a change in a variable?
Because a while loop wastes CPU cycles
What are the operations associated with condition variables in C?
- wait()
- signal()
What is an important thing to keep in mind when signaling or waiting a condition variable?
That you must hold a lock while doing it. This is to avoid race conditions.
What is the producer/consumer problem?
It is when you have one or more producers, and more than one consumer for a single buffer. When the buffer is full and the condition variable that says if the buffer is full or not is changed, the first consumer might be notified that it can now run to consume the buffer. But before that gets to run (because of the scheduler) the second consumer might run and consume everything. Suddenly when the first consumer finally gets to run there are nothing left in the buffer, which is not what it expects.
What is a recommendation to always use when dealing with condition variables in general?
While loops instead of if statements
How do you make sure that a consumer will only wake up producers (and vice-versa)?
By using two condition variables instead of a shared one
How can you assure that all threads waiting on a condition variable will be woken up?
By using broadcast() instead of signal()
How many threads does signal() wake up?
At least one (but usually only one)