In: Computer Science
Explain the need for two CVs to address synchronization between multiple consumers and producers.
Here CV stands for Condition variable.
Condition variable is used for a thread to wait for some condition to be true before continuing. The implementation is quite simple compared to lock, yet the difficult part is to understand how a CV is supposed to used.
CV Interface ::
Condition variable has two interfaces: cv_wait
and
cv_signal
. cv_wait
is used to wait for a
condition to be true, and cv_signal
is used to notify
other threads that a certain condition is true.
a. If the pool is full, then producers can not put to the pool b. If the pool is empty, then consumers can not take stuff from the pool.
Here we also use a lock to protect access to the pool. We can see from this example:
a. Condition variable is virtually a wait channel
b. Condition variable is normally used together with lock, but condition variable itself doesn't contain a lock
Do let me know if need any further assiatance. Thanks