Reputation: 2632
For example, in pseudocode:
lock mutex1
pthread_cond_wait(condition, mutex1)
unlock mutex1
...from another thread:
lock mutex2
pthread_cond_signal(condition)
unlock mutex2
Does the calling thread have to have a lock on the same mutex that is waiting? It seems to me that this would limit the use of condition variables, when you have many threads running at the same time that would like to communicate with each other.
Upvotes: 0
Views: 1034
Reputation: 249123
mutex2
is irrelevant to your example, you may as well not hold any mutex in the notifier.
The docs say:
pthread_cond_signal() may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() have associated with the condition variable during their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal().
It doesn't sound like you require any specific wakeup scheduling behavior, so yes you can signal the condition variable without holding any mutex at all, or when holding unrelated mutexes.
Upvotes: 4