Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q: Read the manual (or any resources) about the implementation of PThread condit

ID: 3844693 • 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

The mutex is employed to safeguard the condition variable itself. That is why you need it locked before you are doing a wait.
The wait can "atomically" unlock the mutex, permitting others access to the condition variable . Then once the condition variable is signalled or broadcast to, one or a lot of of the threads on the waiting are woken up and therefore the mutex are as if by magic fastened once more for that thread. once the thread has been flagged to prevent doing work the loop can exit, the mutex are unlocked and this thread can exit.