Why do we need a condition check before pthread_cond_wait

后端 未结 2 1590
陌清茗
陌清茗 2020-12-15 06:37

I am trying to learn basics of pthread_cond_wait. In all the usages, I see either

if(cond is false)
   pthread_cond_wait

or



        
2条回答
  •  醉酒成梦
    2020-12-15 07:04

    You must test the condition under the mutex before waiting because signals of the condition variable are not queued (condition variables are not semaphores). That is, if a thread calls pthread_cond_signal() when no threads are blocked in pthread_cond_wait() on that condition variable, then the signal does nothing.

    This means that if you had one thread set the condition:

    pthread_mutex_lock(&m);
    cond = true;
    pthread_cond_signal(&c);
    pthread_mutex_unlock(&m);
    

    and then another thread unconditionally waited:

    pthread_mutex_lock(&m);
    pthread_cond_wait(&c, &m);
    /* cond now true */
    

    this second thread would block forever. This is avoided by having the second thread check for the condition:

    pthread_mutex_lock(&m);
    if (!cond)
        pthread_cond_wait(&c, &m);
    /* cond now true */
    

    Since cond is only modified with the mutex m held, this means that the second thread waits if and only if cond is false.

    The reason a while () loop is used in robust code instead of an if () is because pthread_cond_wait() does not guarantee that it will not wake up spuriously. Using a while () also means that signalling the condition variable is always perfectly safe - "extra" signals don't affect the program's correctness, which means that you can do things like move the signal outside of the locked section of code.

提交回复
热议问题