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
6
votes
4 answers

std::condition_variable spurious blocking

As you know, condition variables should be called in cycle to avoid spurious wake-ups. Like this: while (not condition) condvar.wait(); If another thread wants to wake up waiting thread, it must set condition flag to true. E.g.: condition =…
Vasily
  • 235
  • 3
  • 14
6
votes
2 answers

Using a C++11 condition variable in VS2012

I can't get code working reliably in a simple VS2012 console application consisting of a producer and consumer that uses a C++11 condition variable. I am aiming at producing a small reliable program (to use as the basis for a more complex program)…
5
votes
1 answer

boost interprocess shared mutex and boost interprocess condition variable for shared mutex

Boost version - 1.47 I can not find boost::interprocess::interprocess_sharable_mutex, but it looks like it is forward declared. Is this really supported ? I can see that boost::interprocess::interprocess_upgradable_mutex is forward declared and it…
5
votes
1 answer

asio::async_write incredibly difficult to synchronize on a high volume stream

I am currently using the Asio C++ library and wrote a client wrapper around it. My original approach was very basic and only needed to stream in a single direction. Requirements have changed and I've switched over to using all asynchronous calls.…
user0000001
  • 2,092
  • 2
  • 20
  • 48
5
votes
1 answer

Using CLOCK_MONOTONIC type in the 'condition variable' wait_for() notify() mechanism

I am using code that runs on ARM (not Intel processor). Running c++11 code example (CODE A) from: http://www.cplusplus.com/reference/condition_variable/condition_variable/wait_for/ to test the wait_for() mechanism. This is not working right - looks…
progtec
  • 61
  • 1
  • 4
5
votes
0 answers

Why does std::condition_variable.wait() not lock again if exception occurs?

According to http://en.cppreference.com/w/cpp/thread/condition_variable/wait: Atomically releases lock, blocks the current executing thread, and adds it to the list of threads waiting on *this. The thread will be unblocked when notify_all() or…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
5
votes
1 answer

What is the overhead associated with std::condition_variable_any

I have read in many places that there is some overhead associated with std::condition_variable_any. Just wondering, what is this overhead? My guess here is that since this is a generic condition variable that can work with any type of lock, it…
Curious
  • 20,870
  • 8
  • 61
  • 146
5
votes
2 answers

Implementing a condition_variable to solve a multithreaded busy-wait

My program prints multiple lines of text to the console through the use of idle worker threads. The problem, however, is that the workers aren't waiting on previous workers to finish before printing the text, which results in text being inserted…
5
votes
2 answers

Using channel or sync.Cond to wait for condition

I am trying to wait for a specific condition, and I would like advice as to how this is done best. I have a struct that looks like this (simplified): type view struct { timeFrameReached bool Rows []*sitRow } In a goroutine, I am updating a…
Alex Parker
  • 1,533
  • 3
  • 16
  • 38
5
votes
2 answers

C++ - Multi-threading - Communication between threads

#include #include #include #include #include #include #include #include using namespace std; //counts every number that is added to the queue static long long…
cmplx96
  • 1,541
  • 8
  • 34
  • 48
5
votes
1 answer

pthreads, mq_timedreceive, pthread_cond_timedwait and CLOCK_REALTIME

I am working with a system that has implemented two posix functions mq_timedreceive() and pthread_cond_timedwait() Both of these functions use an absolute timeout based on CLOCK_REALTIME. This clock is getting changed at various times during system…
trench_digger
  • 285
  • 2
  • 8
5
votes
4 answers

std::condition_variable wait() and notify_one() synchronization

Preface: I've seen similar questions here, but not one of them seems to answer my question. Is there a reliable way to make sure that wait() method in consumer thread is called before the first notify_one() call from the producer thread? Even with…
Vlad Fedyaev
  • 105
  • 1
  • 9
5
votes
2 answers

Passing a dummy lock to std::condition_variable_any::wait

Suppose there are three threads A, B, and C. B and C suspend at a certain point, waiting for A to signal them to continue. Among the thread synchronization facilities provided by standard C++, std::condition_variable seems to best fit in here…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
5
votes
2 answers

std::condition_variable::wait_for exits immediately when given std::chrono::duration::max

I have a wrapper around std::queue using C++11 semantics to allow concurrent access. The std::queue is protected with a std::mutex. When an item is pushed to the queue, a std::condition_variable is notified with a call to notify_one. There are two…
Tim Flynn
  • 53
  • 1
  • 3
5
votes
1 answer

Delete std::condition_variable_any directly after notify_all crashes

I have a portion of code, where one thread calls something like: cond->notify_all(); delete cond; with std::condition_variable_any cond; Afaik, this should work, since I should be allowed to delete the condition variable, as soon as I notified all…
Ongy
  • 271
  • 3
  • 13