Reputation: 1
Does the pthread_cond_wait() unlock all the waiting mutexes, or just a specific mutex? If so, why doesn't it unlock a mutex from the same kind of thread? What i mean is that in case the next waiting mutex comes from a different thread of the same function, why would the pthread_cond_wait() not unlock that one instead, but it unlocks the thread from where the signal will be broadcasted afterwards? First time writing in stackoverflow so hope I was clear enough.
Upvotes: 0
Views: 26
Reputation: 180048
Does the pthread_cond_wait() unlock all the waiting mutexes, or just a specific mutex?
pthread_cond_wait()
unlocks only the specific mutex passed to it. Additionally, the calling thread must hold that mutex locked at the time of the call, and the same mutex must be used with the particular CV in all pthread_cond_wait()
-family operations. After being awakened, the calling thread will not return from the wait until it reacquires the mutex, so once it does return, it will hold all the same mutexes that it did at the time of the call.
If so, why doesn't it unlock a mutex from the same kind of thread? What i mean is that in case the next waiting mutex comes from a different thread of the same function, why would the pthread_cond_wait() not unlock that one instead, but it unlocks the thread from where the signal will be broadcasted afterwards?
Only one mutex can be used with any given CV, else undefined behavior results. Since every thread waiting on that CV will present the same mutex, the question is moot -- there is no alternative mutex to consider unlocking.
Upvotes: 1