Generally speaking, pthread_cond_wait() and pthread_cond_signal() are called as below:
//thread 1:
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
do_something()
pthread_mutex_unlock(&mutex);
//thread 2:
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
The steps are
pthread_cond_wait(&cond, &mutex);is called, it unlocks the mutexThread 2 locks the mutex and calls
pthread_cond_signal(), which unlocks the mutexIn thread 1,
pthread_cond_wait()is called and locks the mutex again
Now in thread 2, after pthread_cond_signal() is called, pthread_mutex_unlock(&mutex) is going to run, it seems to me that it wants to unlock a the mutex which is now locked by thread 1. Is there anything wrong in my understanding?
Besides, it also seems to me that pthread_cond_wait() can be called by only 1 thread for the same cond-mutex pair. But there is a saying "The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond)." So, it means pthread_cond_wait() can be called by many threads for the same cond-mutex pair?