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

前端 未结 5 1174
星月不相逢
星月不相逢 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:48

    If you do not care that this signal will be lost - there is no error. It is only an error if you expect later coming waiting thread to wake from cond_wait() immediately.

    Since this is usual use case for pthread_cond, tutorial calls this logical error. But nothing will crash and no unexpected behavior will occur. In normal execution flow cond_signal() still may be issued when there is no threads in cond_wait(): f.e., all readers may be just doing message processing when writer adds another data piece in queue.

提交回复
热议问题