pthread_cond_wait and mutex requirement

前端 未结 3 742
悲哀的现实
悲哀的现实 2020-12-23 10:41

Why it is required to lock a mutex before calling pthread_cond_wait?

Also, is it required to take a lock (on the same mutex) before calling pthrea

3条回答
  •  心在旅途
    2020-12-23 11:07

    Condition variables are for synchronising on a condition that you are expecting to change. Locking ensures that:

    • The change can be reliably observed on the waiting threads
    • The item under change isn't somehow changed by another thread at the same time one of the now-woken-up threads is observing it

    A condition system that doesn't use mutexes would be much more brittle.

提交回复
热议问题