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
15
votes
2 answers

Threadpool implementation: condition_variables vs. yield()

I try to develop a threadpool in C++ and I wonder if it is better to yield() the thread in the main loop of the worker thread or to wait on a condition variable: void worker_thread( void ) { // this is more or less pseudocode while( !done ) …
headmyshoulder
  • 6,240
  • 2
  • 20
  • 27
15
votes
2 answers

Do I need to synchronize std::condition_variable/condition_variable_any::notify_one

Do I need to synchronize std::condition_variable/condition_variable_any::notify_one? As far as I can see, if lost of notifications is acceptable - it is OK to call notify_one not protected (by mutex for example). For instance, I saw following usage…
qble
  • 1,256
  • 2
  • 12
  • 29
15
votes
2 answers

Using std::mutex, std::condition_variable and std::unique_lock

I'm having some trouble understanding condition variables and their use with mutexes, I hope the community can help me with. Please note, I come from a win32 background, so I'm used with CRITICAL_SECTION, HANDLE, SetEvent, WaitForMultipleObject,…
fishfood
  • 4,092
  • 4
  • 28
  • 35
14
votes
4 answers

What is the consensus number for semaphores?

(I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)?
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
13
votes
2 answers

why does a conditional variable fix our power consumption?

We were working on our audio player project on mac and noticed that the power usage was so high (about 7x that of google chrome doing the same workload.) I used xcode's energy profiling tool, one of the problems was we had too much cpu-wake…
Bill Yan
  • 3,369
  • 4
  • 27
  • 42
13
votes
2 answers

What happens if i call wait on a notified condition variable

Suppose I have two threads and one shared c++ 11 condition variable. What whould happen if thread1 call notify and after that thread2 call wait? Will thread2 block forever or it will continue it's work due to call of notify by thread1? Edit: enum…
MRB
  • 3,752
  • 4
  • 30
  • 44
12
votes
2 answers

condition_variable wait_for in C++

I am working with condition_variable on Visual studio 2019. The condition_variable.wait_for() function returns std::cv_status::no_timeout without any notification. #include #include #include #include…
lfybt
  • 123
  • 1
  • 5
12
votes
2 answers

Why does std::condition_variable as a class member cause compile errors with std::thread?

I tried including std::condition_variable as a class member, and got a lot of compilation errors when passing an object of this class to a std::thread. I cut down all the other code from my actual program, and ended up with the below minimal code.…
Masked Man
  • 1
  • 7
  • 40
  • 80
12
votes
1 answer

std::promise/std::future vs std::condition_variable in C++

Signaling between threads can be achieved with std::promise/std::future or with good old condition variables. Can someone provide examples/use case where one would be a better choice over the other ? I know that CVs could be used to signal multiple…
12
votes
2 answers

Why do I need to acquire a lock to modify a shared "atomic" variable before notifying condition_variable

Accoding to cppreference.com: The thread that intends to modify the variable has to acquire a std::mutex (typically via std::lock_guard) perform the modification while the lock is held execute notify_one or notify_all on the…
kzhdev
  • 667
  • 5
  • 14
12
votes
3 answers

Is it guaranteed that pthread_cond_signal will wake up a waiting thread?

This is a general question. For example, currently two child threads have called pthread_cond_wait(&cond1,&mutex), and they are both waiting. Then, the parent thread calls pthread_cond_signal(&cond1); pthread_cond_signal(&cond1); Next, my question…
tgeng
  • 1,768
  • 2
  • 15
  • 20
11
votes
1 answer

How To Use Condition Variable

The Linux Programming Interface book has a piece of code (producer/consumer) to show how condition variable works: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int avail =…
Majid Azimi
  • 5,575
  • 13
  • 64
  • 113
11
votes
6 answers

why does std::condition_variable::wait need mutex?

TL;DR Why does std::condition_variable::wait needs a mutex as one of its variables? Answer 1 You may look a the documentation and quote that: wait... Atomically releases lock But that's not a real reason. That's just validate my question even…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
11
votes
1 answer

c++ condition variable notification not working as expected

I'm trying to launch new threads as soon as work in previous worker_thread has started, but maybe ended or not. I've replaced started and ended work with time delays. My code is: #include #include #include #include…
11
votes
2 answers

Exception handling for and

Assuming no undefined behaviour occurs, no deadlocks occur, mutexes are locked and unlocked in the correct order by the correct threads the correct number of times, non-recursive mutexes are not locked multiple times, locking recursive mutexes does…
jotik
  • 17,044
  • 13
  • 58
  • 123
1 2
3
47 48