Can pthread_cond_wait() always win the competition in locking a mutex?

后端 未结 2 1009
面向向阳花
面向向阳花 2021-01-07 11:36

This question is regarding the pthread tutorial in llnl. Say there are three threads.

Thread 1:

pthread_mutex_lock(&mutex)
do_s         


        
2条回答
  •  误落风尘
    2021-01-07 11:48

    There is no guarantee. Returning from pthread_cond_wait() should be treated as implying that the condition might have changed, not that it definitely has - so you need to re-check the condition, and may need to wait again. This is exactly why pthread_cond_wait() should be called within a loop that checks the condition.

提交回复
热议问题