Pthread_cond_wait and mutex requirement

Why is a mutex lock required before calling pthread_cond_wait ?

In addition, is it necessary to make a lock (in the same mutex) before calling pthread_cond_signal ?

thanks for your help.

+16
pthreads posix
Jun 10 2018-11-18T00:
source share
3 answers

Why is a mutex lock required before calling pthread_cond_wait?

Because otherwise there is an inevitable race condition.

Mutex protects shared state. A condition variable is associated with some predicate ("condition") in the state. The basic idea is that you want:

1) check the predicate

2) if the predicate is false, go to sleep mode until it becomes true.

In a parallel system, for some flow it is always possible to make the predicate true between (1) and (2). To avoid this race, you must hold the mutex to (1), and you must atomize it while doing (2).

For example, for a queue, the predicate may be โ€œqueue is not emptyโ€. But meanwhile, as you check to see if the queue is in turn and the time when you go to sleep, some other thread may add something to the queue.

Thus, you must keep the mutex both when checking the predicate and when calling pthread_cond_wait.

In addition, is it necessary to make a lock (in the same mutex) before calling pthread_cond_signal?

As far as I know, there is no fundamental problem with this; it simply introduces potential inefficiency.

Here again, any shared state that you change (and thus the predicate true) must be protected by a mutez. Therefore, anytime you want to signal a status, you should still have a mutex.

If you release the mutex before the condition signal, the predicate may become false between them due to the action of some other thread. This race does not crash because any thread waiting for a state must in any case double-check the predicate before continuing ... But why did it cause a problem?

Bottom line: just follow the instructions and you donโ€™t even need to think about these issues. :-)

+25
Jun 10 2018-11-11T00:
source share

The condition variables are intended to be synchronized under the condition that you expect to change. Locking ensures that:

  • Change can be reliably observed in waiting threads
  • The element under change does not change in any other flow, while one of those who are now awakening observes it.

A condition system that does not use mutexes will be much more fragile.

+1
Jun 10 2018-11-11T00:
source share

The whole point of condition variables is to allow threads to receive notifications of changes in data structures protected by a mutec, for example. you might want to be notified when the queue is no longer empty, so you atomically release the mutex and wait for the condition from the variable, and when the new element is queued, you wake up and accept the mutex to process the new element.

Unlike Java monitors, pthread_cond_{signal,broadcast}() does not need to hold mutexes. The signal transmission of a condition variable when the thread does not expect a condition from this variable is lost, but this should not be much different, since the signal can also be lost if the manufacturer starts working before the consumer.

+1
Jun 10 2018-11-11T00:
source share



All Articles