I'm having some trouble wrapping my head around why a while loop is used for pthread_cond_wait. Let's take a simple example. Here's some worker thread:
pthread_mutex_lock (&loadingLock);
while (isReadyToLoad == false){
pthread_cond_wait(&readyCondition, &loadingLock);
}
pthread_mutex_unlock (&loadingLock);
So, now the master thread does two things:
- Sets isReadyToLoad to true
- pthread_cond_signal(&readyCondition);
This is how I typically see the framework of pthreads laid out in examples. My question is how the while loop is valid at all. When I try and trace the logic, I get the following steps:
- Master sets isReadyToLoad to true
- X is no longer true for while(x) in the worker thread
- The while loop therefore is broken out of, meaning that pthread_cond_wait(&readyCondition, &loadingLock) is bypassed and will never receive the signal
Clearly my logic is flawed somewhere along the way, but I'm not sure how I should be thinking about this instead, and would appreciate any clarification.