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
11
votes
5 answers

How does condition_variable::wait_for() deal with spurious wakeups?

Spurious wakup is allowed by various platforms. To counter that, we write below looping mechanism: while(ContinueWaiting()) cv.wait(lock); // cv is a `std::conditional_variable` object Same thing is understandable for…
iammilind
  • 68,093
  • 33
  • 169
  • 336
11
votes
3 answers

What happens when calling the destructor of a thread object that has a condition variable waiting?

I am using a SynchronisedQueue to communicate between threads. I found that destroying the thread object when the attaching thread is waiting on a condition variable would cause the program crash. This can be corrected by calling detach() before the…
WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
11
votes
3 answers

Does a mutex get unlocked when calling notify on a condition variable?

I am trying to understand what happens to a mutex when it is used in a condition variable. In the following example, taken from cppreference int main() { std::queue produced_nums; std::mutex m; std::condition_variable cond_var; …
danny
  • 319
  • 3
  • 9
10
votes
1 answer

Reusing a unique_lock in a consumer loop

I stumbled over the following code in Bjarne Stroustrup's "The C++ Programming Language, 4th Edition" on page 119: queue mqueue; condition_variable mcond; mutex mmutex; void consumer() { while(true) { unique_lock
Bobface
  • 2,782
  • 4
  • 24
  • 61
10
votes
3 answers

Is possible to use std::condition_variable with std::lock_guard?

I am using a std::condition_variable combined with a std::unique_lock like this. std::mutex a_mutex; std::condition_variable a_condition_variable; std::unique_lock a_lock(a_mutex); a_condition_variable.wait(a_lock, [this] {return…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
10
votes
1 answer

Can std::condition_variables be used as counting semaphores?

This is a follow-up to Can C++11 condition_variables be used to synchronize processes?. Can std::condition_variable objects be used as counting semaphores? Methinks not because the object seems bound to a std::mutex, which implies it can only be…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
10
votes
3 answers

c++ condition_variable wait_for predicate in my class, std::thread error

I am trying to use a thread within my class, then the thread needs to use a condition_variable, and the condition variable will be blocked until a predicate be changed to true. The code looks like this: class myThreadClass{ bool bFlag; …
user3375009
  • 117
  • 1
  • 4
10
votes
2 answers

std::condition_variable::wait with predicate

In the documentation for std::condition_variable, there is an overload of wait() taking as argument a predicate function. The function will wait until the first wake_up at which the predicate function is true. In the documentation It is stated that…
galinette
  • 8,896
  • 2
  • 36
  • 87
10
votes
1 answer

Why does Windows have no DeleteConditionVariable() function to go together with InitializeConditionVariable()?

I'm trying out Windows support for Condition Variables today (as provided by Microsoft for Windows Vista and later). To initialize a condition variable, I call InitializeConditionVariable(), which is straightforward enough, but I don't see any way…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
9
votes
4 answers

C++17 atomics and condition_variable deadlock

I have the following code, which deadlocks on the commented lines. Basically f1 and f2 run as individual threads in the program. f1 expects i to be 1 and decrements it, notifying the cv. f2 expects i to be 0 and increments it, notifying the cv. I…
user1413793
  • 9,057
  • 7
  • 30
  • 42
9
votes
1 answer

libc++ implementation of std::condition_variable_any

Condition variables should have have a single order with respect to notify() and unlock_sleep() (an imaginary function call used within wait() where the mutex is unlocked and the thread sleeps as one atomic sequence of operations) operations. To…
Curious
  • 20,870
  • 8
  • 61
  • 146
9
votes
1 answer

Which OS / platforms implement wait morphing optimization?

Which major OS / platforms implement wait morphing? This question came up when I noticed that there's no clearcut best practice about whether one should signal a condition variable with mutex locked or not. A typical recommendation is to signal…
max
  • 49,282
  • 56
  • 208
  • 355
9
votes
2 answers

signal on condition variable without holding lock

So I just found out that it's legal to signal a condition variable if you're not holding the lock in c++11. That seems to open the door to some nasty race condition: std::mutex m_mutex; std::condition_variable m_cv; T1: …
Voo
  • 29,040
  • 11
  • 82
  • 156
9
votes
4 answers

C++11 Thread: Multiple threads waiting on a condition variable

I am currently working on a problem that simulates a extended Producer-Worker model. In this problem there are 3 workers and 3 tools available, and for workers to work they need 2 tools (and materials but those are irrelevant). If there are >=2…
user1745746
  • 103
  • 1
  • 1
  • 6
8
votes
1 answer

Why do we need an empty std::lock_guard before doing condition variable notify?

I am currently studying Google's Filament job system. You can find the source code here. The part that confuses me is this requestExit() method: void JobSystem::requestExit() noexcept { mExitRequested.store(true); { std::lock_guard
kevinyu
  • 1,367
  • 10
  • 12