condition variable - why calling pthread_cond_signal() before calling pthread_cond_wait() is a logical error?

前端 未结 5 1142
星月不相逢
星月不相逢 2020-11-27 17:25

It\'s written in POSIX threads tutorial https://computing.llnl.gov/tutorials/pthreads/ that it is a logical error.

my question is why it is a logical error?

5条回答
  •  自闭症患者
    2020-11-27 17:38

    My 2 cents: I do not know the side effects of calling *pthread_cond_signal()* when no thread has been blocked calling *pthread_cond_wait()*. This is really an implementation detail What I think is that, if your threading/timimg model do not guarantee the rigth order between wait and signal, probably you should consider a different sync mechanism [like a simple semaphore, for example] when you can signal the semaphore from thread B even if the thread A has nor reached the sync point. When thread A will reach the sync point, it will find the semaphore incremented and will enter the critical session.

提交回复
热议问题