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

C++11 - can't awake a thread using std::thread and std::condition_variable

I'm stuck on a problem when trying to awake a thread by another one. A simple producer / consumer thing. Below the code. Line 85 is the point I don't understand why it's not working. The producer thread fills up a std::queue and calls…
user1587451
  • 978
  • 3
  • 15
  • 30
5
votes
1 answer

Boss Worker Pthreads Web Server in C - Server crashes if more requests sent than number of threads

I'm writing a web server in C (which I suck with) using Pthreads (which I suck with even more) and I'm stuck at this point. The model for the server is boss-worker so the boss thread instantiates all worker threads at the beginning of the program.…
Nate
  • 131
  • 2
  • 7
5
votes
2 answers

Python condition variable timeout

I have thread1 which is waiting on a condition from thread2. But it could be that thread2 is never signalling the condition variable. So I have added a timeout to the wait call in thread 1, like this: cv.acquire() cv.wait(1.0) cv.release() How can…
blueFast
  • 41,341
  • 63
  • 198
  • 344
4
votes
1 answer

Are std::condition_variable wait predicates thread-safe?

Take this code: std::condition_variable var; var.wait(lock, [&sharedBool] { return sharedBool; }); When var reads from sharedBool, is that thread safe? If it isn't, is making sharedBool a std::atomic reasonable?
TwistedBlizzard
  • 931
  • 2
  • 9
4
votes
1 answer

Understanding condition_variable::wait for blocking a thread

While implementing a thread pool pattern in C++ based on this, I came across a few questions. Let's assume minimal code sample: std::mutex thread_mutex; std::condition_variable thread_condition; void thread_func() { std::unique_lock
Rhino R.
  • 81
  • 7
4
votes
2 answers

How is CONDITION_VARIABLE implemented?

A longer version of the title question would be: On my machine, sizeof(std::condition_variable) is 72 bytes. What are these 72 bytes used for? Note: The size of std::condition_variable depends on the implementation. Some examples sizes are given…
4
votes
2 answers

C++ atomic is it safe to replace a mutex with an atomic?

#include #include #include #include using namespace std; const int FLAG1 = 1, FLAG2 = 2, FLAG3 = 3; int res = 0; atomic flagger; void func1() { for (int i=1; i<=1000000; i++) { while…
Huy Le
  • 1,439
  • 4
  • 19
4
votes
1 answer

Use of Promise and Future to provide continous periodic notifications from one thread to another

I have two threads. One thread acts as a timer thread which at regular intervals of time needs to send a notification to another thread. I am planning to use C++ Promise and Future for this purpose instead of C++ condition variables. I have the…
bobbydev
  • 525
  • 3
  • 12
4
votes
1 answer

How can multiple threads (that are waiting on a condition variable) acquire the relevant lock when condition_all() is called?

From theory, a waiting thread (let's say Thread_1) first acquires a mutex, then waits on the condition variable by calling wait(). The call to wait() immediately unlocks the mutex. When the other thread (let's say Thread_2) calls notify(), the…
4
votes
3 answers

Condition variable basic example

I am learning condition variables in C++11 and wrote this program based on a sample code. The goal is to accumulate in a vector the first ten natural integers that are generated by a producer and pushed into the vector by a consumer. However it does…
Itsbananas
  • 569
  • 1
  • 4
  • 12
4
votes
1 answer

How to fix "use of deleted function" when using mutex and condition variable as member?

I am doing some multi-threading exercise and couldn't get this code pass compilation. I search online but so far not exactly sure about the cause. #include #include #include #include #include…
user180574
  • 5,681
  • 13
  • 53
  • 94
4
votes
3 answers

What are the costs of a condition variable?

Assume that unused execution resources are available on the machine in question, i.e. not all CPUs are being utilized. If a thread is waiting on a condition variable, what are the costs associated with waking up this thread? Similarly, what are…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
4
votes
2 answers

Condition variables

I noticed that when I'm performing a wait operation on a condition variable, it immediately returns. The consequence is that, when executing the following dummy code, 100% of one CPU is being used in the loop : int main(void)…
4
votes
1 answer

Why 'wait with predicate' solves the 'lost wakeup' for condition variable?

I am trying to understand the difference between spurious vs lost wakeup in case of a condition variable. Following is small piece code I tried. I understand that 'consumer' in this case could wake up without any notification and therefore the wait…
4
votes
1 answer

Using a single Condition Variable to pause multiple threads

I have a program that starts N number of threads (async/future). I want the main thread to set up some data, then all threads should go while the main thread waits for all of the other threads to finish, and then this needs to loop. What I have atm…