Q: Read the manual (or any resources) about the implementation of PThread condit
ID: 3845048 • Letter: Q
Question
Q: Read the manual (or any resources) about the implementation of PThread condition variables and give answer to following questions:
1. Why the condition variable requires a mutex?
2. What happens if a thread is woken up (from wait) but cannot acquire the lock?
3. When signal happens for a condition, and if more than one thread are waiting for that condition:
a. How many threads will be woken up?
b. Could you ensure an order among the threads for waking up? If yes, how?
c. How can you make all the threads to wake up?
Explanation / Answer
1. The condition variable requires mutex because it is used to protect itself. So, it must be locked before wait.
2. The wait will "atomically" unlock the mutex, permitting others to access the condition variable. Then when the condition variable is transmitted, one or more of the threads on the waiting list will be awakened and the mutex will be mysteriously locked again for that thread.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.