October 2018
Beginner
794 pages
19h 23m
English
The correct approach is to use a CV. A condition variable is a way for threads to synchronize upon the value of data in an efficient manner. It achieves the same end result as the naive polling approach does, but in a far more efficient and, even more importantly, correct manner.
Check out the following table:
| Time | Thread B | Thread A |
| t0 | Initialize: gWorkDone = 0 ; init the {CV, mutex} pair | < common > |
| t1 | Wait upon signal from thread B : lock the associated mutex; pthread_cond_wait() | |
| t2 | Perform the work ... | < ... blocking ... > |
| t3 | Work done; lock the associated mutex; signal thread A : pthread_cond_signal() ; unlock the associated mutex | ... |
| t4 | Unblocked; check to see that the work is really done, ... |
Read now
Unlock full access