I have a question regarding use of pthread condition variables. As a general use case is like this
//thread 1:
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
do_something()
pthread_mutex_unlock(&mutex);
//thread 2:
pthread_cond_signal(&cond);
Now I know that pthead_cond_wait will unlock mutex and go to sleep and when awaken it will lock the mutex and then return from call but how to ensure that signal from thread 2 reaches thread 1 after thread 1 is in condition wait. It could occur that thread2 runs first and then thread1 causing lost wake up.
If I use mutex lock in thread 2 again then also it may happen that thread2 will get lock it will signal and thread1 is still trying to acquire lock. This will again result in lost wake up.
How to ensure that signal from condition variable reaches thread waiting on that ?
Thanks