So I have searched high and low on stack overflow and other resources but I am unable to understand a few things with regards to the aforementioned functions. Specifically,
1)When pthread_cond_timedwait() returns because a timer value has run out, how does it automatically re-acquire the mutex. The mutex could potentially be locked elsewhere. For instance, in say a producer-consumer queue, the consumer could be waiting using the timedwait() variant. From explanations I have read, it appears that the mutex will automatically be re-acquired when the thread wakes up. This baffles me? Or is it that it will wake up after the timer runs out and then check the predicate and lock again. That makes no sense?
2)When a bunch of threads have acquired a mutex and are now all waiting on a simple pthread_cond_wait(), what will happen if another thread issues a pthread_cond_broadcast(). Will each of the threads (as determined by the scheduler) wake up one by one, acquire the mutex in sequence and then each of them proceed? Or will only one thread of the entire list of threads wake up. I would assume that the latter of the two behaviors would be possible using pthread_cond_signal() as well. Also, if the previous approach is the right behavior, then we can assume that some other thread might end up calling wait() on the variable somewhere else. Would that then put all these other waiting threads back into a blocked state and waiting for a signal. Or would each of them continue to wake up and make progress one by one?