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
3
votes
1 answer

C++20 semaphore in queue application seems slow compared to condition variable

For study purposes, I’m comparing implementations of single producer single consumer queues. So I compared a condition variable implementation with a C++20 counting semaphore implementation. I would have guessed that the semaphore implementation…
3
votes
0 answers

Is notifiy_one()/notify_all() semantically a release-operation, and if yes, where is that defined?

Consider the following example from cppreference: std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void worker_thread() { // Wait until main() sends data std::unique_lock
JMC
  • 1,723
  • 1
  • 11
  • 20
3
votes
0 answers

clang thread sanitizer reports issues when using conditon variables

Here is a simple example for using std::condition_variable. When using clang+tsan for building the following code, #include #include #include #include #include #include class…
3
votes
1 answer

Is this use of condition variable safe (taken from cppreference.com)

Is this code taken from Example of Producer/Consumer with condition variable safe? Me and a co-worker had a disagreement whether the omission of std::unique_lock lock(m) in close() is safe. The line done = true; is set without a lock. Can the…
Nisse Hult
  • 33
  • 4
3
votes
1 answer

std::conditional_variable notify() problem on Windows 7 called from destructor of static variable

I've faced a problem with std::conditional_variable notify() or notify_all() on Windows 7 when it called from destructor of static variable. The object looks like this (logic is simplified): class SomeObject { public: ~SomeObject() { …
3
votes
1 answer

How does std::condition_variable::wait() evaluate the given predicate?

Context: In every examples I can see about the use of std::condition_variable::wait(), including those coming from cppreference.com, there is never any synchronization mechanism used to protect the predicate evaluation against data races. For…
Fareanor
  • 5,900
  • 2
  • 11
  • 37
3
votes
0 answers

std::condition_variable sensitive to the system clock

I have some code that uses condition_variable. When the system clock gets set backwards, the condition_variable will wait for much longer because it uses system_clock instead of steady_clock. I have seen some workarounds posted here for this issue.…
JVW
  • 51
  • 4
3
votes
1 answer

0 as a timeout in std::condition_variable::wait_for

For instance I have next code: void waitForResponse(const std::optional& ms){ std::unique_lock lk{_mtx}; _cv.wait_for(lk, std::chrono::milliseconds{ms ? *ms : 0}, []() { return someCondition; } } Is it specified…
Igor
  • 1,029
  • 9
  • 21
3
votes
1 answer

POSIX condition variable & mutex "competition"

When a thread waits on a condition variable, the associated mutex is (atomically) released (unlocked). When that condition variable is signaled (by a different thread), one (for signal) or all (for broadcast) waiting thread(s) is/are awakened,…
ags
  • 719
  • 7
  • 23
3
votes
1 answer

Can multiple threads wait on the same condition variable?

I'm trying to understand how to better use condition variables, and I have the following code. Behavior. The expected behavior of the code is that: Each thread prints "thread n waiting" The program waits until the user presses enter When the user…
Alecto Irene Perez
  • 10,321
  • 23
  • 46
3
votes
2 answers

How to use std::condition_variable in a loop

I'm trying to implement some algorithm using threads that must be synchronized at some moment. More or less the sequence for each thread should be: 1. Try to find a solution with current settings. 2. Synchronize solution with other threads. 3. If…
Krzysztof
  • 769
  • 8
  • 27
3
votes
2 answers

can pthread_cond_signal make more than one thread to wake up?

I'm studying on condition variables of Pthread. When I'm reading the explanation of pthread_cond_signal, I see the following. The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified…
3
votes
3 answers

Multi-thread can't join properly

#include #include #include #include #include #include using namespace std; mutex m; condition_variable cov; bool ready = false; bool processed = false; void showNum(int &f_,…
3
votes
2 answers

Conditional variable is stuck on waiting

Why the condition variable is stuck on waiting if it was notified in worker_thread? What am I missing here? #include #include #include #include std::mutex m; std::condition_variable cv; void…
theateist
  • 13,879
  • 17
  • 69
  • 109
3
votes
1 answer

Why does pthread_cond_wait() not block when not linked with "-lpthread"?

I'm learning about pthread_cond_t and wrote the following code intended to block forever at the pthread_cond_wait(): // main.cpp // Intentionally blocks forever. #include #include #include #include int…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86