Questions tagged [condition-variable]

A synchronisation primitive used in multithreaded programming to wait for a condition to be true.

In a multithreaded program it often happens that a thread cannot proceed until some condition is met, such as another thread completing a task, or providing some input to be processed. Instead of wasting CPU cycles by constantly checking, a condition variable can be used to make the thread go to sleep until the condition is met.

Using a condition variable requires three things: the condition variable itself, a lock (e.g. a mutex or critical section) that prevents other threads from modifying the data being used, and a predicate to test the condition being waited for (e.g to answer "has the other thread finished?" or "is there input to be processed?")

When calling a condition variable's "wait" function it will atomically release the lock and block the calling thread (it must be atomic so that there is no window where the condition could become true and the thread would miss the notification and sleep forever.) The thread will be unblocked when another thread notifies that the condition is true, at which point the condition variable reacquires the lock and the caller should test the predicate to check the condition.

Examples of condition variable types provided by different APIs:

  • C++11 : std::condition_variable and std::condition_variable_any
  • Boost : boost::condition_variable and boost::condition_variable_any
  • POSIX : pthread_cond_t
  • Win32 : (since Vista) CONDITION_VARIABLE

See also:

712 questions
4
votes
2 answers

implementation of std::condition_variable::wait_until

I am reading the libstdc++ implementation of std::condition_variable::wait_until, here is the source: template cv_status wait_until(unique_lock& __lock, const chrono::time_point<_Clock,…
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
4
votes
1 answer

why does pthread_cond_signal cause deadlock

I am new to conditional variables and get deadlock if not using pthread_cond_broadcast(). #include #include pthread_mutex_t m_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cv = PTHREAD_COND_INITIALIZER; bool ready =…
luyi0619
  • 313
  • 3
  • 8
4
votes
1 answer

Where does the wait queue for threads lies in POSIX pthread mutex lock and unlock?

I was going through concurrency section from REMZI and while going through mutex section, and I got confused about this: To avoid busy waiting, mutex implementations employ park() / unpark() mechanism (on Sun OS) which puts a waiting thread in a…
pankaj
  • 1,316
  • 3
  • 16
  • 27
4
votes
2 answers

Is there an alternative to the threading.Condition variables in python that better support timeouts without polling?

I'm using condition variables in threads that require a timeout. I didn't notice until I saw the CPU usage when having a lot of threads running, that the condition variable provided in the threading module doesn't actually sleep, but polls when a…
goji
  • 6,911
  • 3
  • 42
  • 59
4
votes
1 answer

Condition Variable - Wait/Notify Race Condition

I'll present some code first since explaining is easier that way. Assume that mutexes are correctly used with the condition variables to keep it simple: // Thread 1 while(1) { conditionVariable.wait(); // Do some work } // Thread…
4
votes
2 answers

The differences between pthread_cond_t and std::condition_variable

I am now testing std::condition_variable recently , and find it is quite different with pthread_cond_t after test , I like to know if anything in my test wrong ? or std::condition_variable is really quite different with pthread_cond_t ? The…
barfatchen
  • 1,630
  • 2
  • 24
  • 48
4
votes
2 answers

Can single condition variable be used for bidirectional synchronization?

Is it possible to use single condition variable for bidirectional synchronization (i.e. two different conditions are waited for at different times on the same condition variable)? I'm sure that no more than one thread will wait on the condition…
user283145
4
votes
1 answer

Shared list, multiple conditions, one or more condition variables?

Consider that you have a list: class CLIENTS { public: CLIENTS(); ~CLIENTS(); bool addClient(); bool removeClient(); bool getDataFromClientObj(unsigned int id); bool storeDataInClientObj(unsigned int id); private: //…
fludd
  • 53
  • 1
  • 6
4
votes
2 answers

std::condition_variable::notify_one() called twice

How many waiting threads will wake up if I call std::condition_variable::notify_one() twice without any time interval, like this: { std::unique_lock lock(condvar_mutex); condvar.notify_one(); condvar.notify_one(); } Is…
Vasily
  • 235
  • 3
  • 14
4
votes
3 answers

POSIX Threads: Condition Variables - what's the point?

I've been working with pthreads a fair bit recently and there's one little thing I still don't quite get. I know that condition variables are designed to wait for a specific condition to come true (or be 'signalled'). My question is, how does this…
Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
4
votes
1 answer

pthread_cond_wait doesn't unlock mutex

I can't find any evidence online of pthread_cond_wait being strange on Mac OS X, but it seems to be failing the simplest test for me. The function int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t * ); is supposed to unlock the mutex argument…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
3
votes
3 answers

Is this usage of condition variables ALWAYS subject to a lost-signal race?

Suppose a condition variable is used in a situation where the signaling thread modifies the state affecting the truth value of the predicate and calls pthread_cond_signal without holding the mutex associated with the condition variable? Is it true…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
3
votes
5 answers

How to use wait_for in a looping thread?

I want to have a thread (std::thread) that does its work every 60 seconds, otherwise sleeps and instantly returns if the outside requests it. std::jthread is not an option, I'm limited to C++14. std::condition_variable cv; //Thread1: void…
Thomas B.
  • 691
  • 4
  • 15
3
votes
3 answers

In POSIX, why can't a single condvar be used with multiple mutexes?

Why is the behavior undefined when a POSIX condition variable is used with multiple mutexes? Excerpt from the 2018 edition: When a thread waits on a condition variable, having specified a particular mutex to either the pthread_cond_timedwait() or…
DannyNiu
  • 1,313
  • 8
  • 27
3
votes
4 answers

What happens to a thread that got woken up by pthread_cond_signal() but lost competition for a mutex

Regarding this: How To Use Condition Variable Say we have number of consumer threads that execute such code (copied from the referenced page): while (TRUE) { s = pthread_mutex_lock(&mtx); while (avail == 0) { /* Wait for something to…
racic
  • 1,235
  • 11
  • 20