This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthread_cond_wait with multiple mutex variables


Bharath Ramesh wrote:
>> From: Jonathan Nieder [mailto:jrnieder@gmail.com]

>> 	When a thread waits on a condition variable, having specified
>> 	a particular mutex to either the pthread_cond_timedwait() or
>> 	the pthread_cond_wait() operation, a dynamic binding is formed
>> 	between that mutex and condition variable that remains in
>> 	effect as long as at least one thread is blocked on the
>> 	condition variable.
[...]
> One thing that I am still unclear about is that you mentioned
> pthread_cond_broadcast uses __data.__mutex to decide what the woken threads
> should wait on. This then should cause all the threads to wait on a
> different mutex than what they really should wait on? Wouldn't this still
> cause undefined behavior?

No, if all waiters specified the same mutex when calling
pthread_cond_wait, it will be the right one.

Ãngel's comment on semantics was much more helpful than mine.  The
idea is that there is some state shared by several threads, protected
from data races by a particular mutex.  When I call pthread_cond_wait,
I am (1) waiting for that state to achieve a certain condition and (2)
releasing the mutex to allow that to happen.

pthread_cond_broadcast can be used to wake up all threads waiting on
the condition.  The mutex will be still locked, so they'll block on
it, and then when it is unlocked each gets to take a turn acting on
the changed state.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]