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

What's the correct way to deal with spurious wakeups, in general?

Among the options below, is there any correct way to deal with spurious wakeups when using conditional variables? 1) Put the wait(unique_lock_ul) into an infinite while loop, using a boolean unique_lock ul(m); while(!full) …
ImAUser
  • 119
  • 1
  • 9
4
votes
1 answer

Multiple vs single condition variable

I'm working on an application where I currently have only one condition variable and lots of wait statements with different conditions. Whenever one of the queues or some other state is changed I just call cv.notify_all(); and know that all threads…
sebi707
  • 360
  • 2
  • 11
4
votes
0 answers

Is the performance of notify_one() really this bad?

For the measurement below I've been using x86_64 GNU/Linux with kernel 4.4.0-109-generic #132-Ubuntu SMP running on the AMD FX(tm)-8150 Eight-Core Processor (which has a 64 byte cache-line size). The full source code can be obtained here:…
Carlo Wood
  • 5,648
  • 2
  • 35
  • 47
4
votes
2 answers

Process-shared condition variable : how to recover after one process dies?

I'm working on a simple FIFO queue to synchronize multiple instances of a server process. This is very similar to Linux synchronization with FIFO waiting queue, except dealing with multiple processes instead of threads. I adapted caf's ticket lock…
lemonsqueeze
  • 1,041
  • 8
  • 19
4
votes
2 answers

How much time it takes for a thread waiting with pthread_cond_wait to wake after being signaled? how can I estimate this time?

I'm writing a C++ ThreadPool implantation and using pthread_cond_wait in my worker's main function. I was wondering how much time will pass from signaling the condition variable until the thread/threads waiting on it will wake up. do you have any…
4
votes
1 answer

How to use a std::condition_variable correctly?

I'm confused about conditions_variables and how to use them (safely). In my application I've a class that makes a gui-thread but while the gui is constructed by the gui-thread, the main thread needs to wait. The situation is the same as for the…
dani
  • 3,677
  • 4
  • 26
  • 60
4
votes
2 answers

Purpose of condition_variable

Application without std::condition_variable: #include #include #include #include #include #include std::mutex mutex; std::queue queue; int counter; void loadData() { …
Akasata Akasata
  • 333
  • 4
  • 10
4
votes
0 answers

How to deal with conditional variables in movable types

There is a great answer how to write move constructors for classes that contain std::mutex. But I wonder what the process is in implementing move constructors for objects containing condition_variable.
ilya1725
  • 4,496
  • 7
  • 43
  • 68
4
votes
1 answer

Proper way to destroy threads waiting on a std::conditional_variable during main program exit

I am using std::conditional_variable for timing a signal in a multi-threaded program for controlling the flow of various critical sections. The program works but during exit I am compelled to use a predicate (kill_ == true) to avoid destroying of…
ark1974
  • 615
  • 5
  • 16
4
votes
1 answer

Condition variable waiting on multiple mutexes

I have a std::condition_variable_any that waits on a custom lock which is a composition of two mutexes (one std::mutex and one shared-locked std::shared_mutex). Its unlock() operation simply unlocks both mutexes sequentially. For example…
tmlen
  • 8,533
  • 5
  • 31
  • 84
4
votes
3 answers

How to wait for 2 types of events in a loop (C)?

I am trying to wait on waitpid() and read() in a while-true loop. Specifically, I am waiting for either one of these two events and then process it in each iteration of the loop. Currently, I have the following implementation (which is not I…
cstjh
  • 153
  • 1
  • 2
  • 8
4
votes
1 answer

Mutex status after spurious wakeup

Consider this basic multithreading program using pthreads. We have a main thread, creating another thread that does some work. bool done = false; mutex m; condition c; void foo() { pthread_mutex_lock(&m); //while(!done) { …
JMC
  • 1,723
  • 1
  • 11
  • 20
4
votes
1 answer

Android: C++ thread not waking up if screen-locked or in background. Works fine when app is in use

In our Android app, we have UI component and core C++11 module. A thread is running based on std::chrono::system_clock::time_point, such as below: while(this->m_ConditionVariable.wait_until(lock, this->m_Object.to_time_point()) ==…
iammilind
  • 68,093
  • 33
  • 169
  • 336
4
votes
1 answer

Why is a condition_variable not MoveAssignable

Why is a condition_variable not MoveConstructible (as per http://en.cppreference.com/w/cpp/thread/condition_variable)? That prohibits inclusion in lots of containers (eg. std::unordered_map) that move things around. This forces people to use a…
Curious
  • 20,870
  • 8
  • 61
  • 146
4
votes
0 answers

C++ condition_variable and spurious wakeup

I'm interested to use the condition_variable to synchronize two threads ( A and B ) that don't share data. A good solution I found surfing the internet is the following one. class WaitableCondition I found it here : Paper link The problem is in two…