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

Does std::promise internally use std::condition_variable to notify the associated std::future?

My question is does std::promise notify the associated std::future through using a std::condition_variable? I search the source code of std::promise and found this website. But I didn't see std::promise has std::condition_variable in its member…
Tes
  • 349
  • 3
  • 12
8
votes
5 answers

unlock the mutex after condition_variable::notify_all() or before?

Looking at several videos and the documentation example, we unlock the mutex before calling the notify_all(). Will it be better to instead call it after? The common way: Inside the Notifier thread: //prepare data for several worker-threads; //and…
Kari
  • 1,244
  • 1
  • 13
  • 27
8
votes
3 answers

`std::condition_variable::wait_for` calls the predicate very often

Consider the following codesnippet: #include #include #include #include int main () { std::mutex y; std::condition_variable x; std::unique_locklock{y}; int i = 0; auto increment…
nnolte
  • 1,628
  • 11
  • 25
8
votes
4 answers

What if the system time changes while I'm doing timed_wait with a duration?

When using timed_wait on a boost::condition_variable with a duration, will the wait condition time out after the duration even if the user (or ntp) changes the system time? E.g., boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1…
indiv
  • 17,306
  • 6
  • 61
  • 82
8
votes
3 answers

When can std::condition_variable be used without a predicate?

If std::condition_variable can be signaled due to the spurious wakeups (and we can't be sure that the condition we need is really satisfied), why do C++ Standard Library provide the overloads of wait() method without a predicate? What are the…
undermind
  • 1,779
  • 13
  • 33
8
votes
3 answers

How do std::unique_lock and std::condition_variable work

I need to be clarified how lock and condition_variable work. In the -slightly modified- code from here cplusplusreference std::mutex m; std::condition_variable cv; std::string data; bool ready = false; bool processed = false; void…
Mert Mertce
  • 1,614
  • 3
  • 21
  • 32
8
votes
2 answers

Is it possible to implement C++11 mutex concept for use by std::condition_variable?

I find that the std::mutex implementation in Visual Studio 2013 is too slow. It uses a heavy weight mutex to assure that synchronization can be achieved even between processes which is all fine and dandy; Unless you're not talking to other processes…
Emily L.
  • 5,673
  • 2
  • 40
  • 60
8
votes
1 answer

Locking C++11 std::unique_lock causes deadlock exception

I'm trying to use a C++11 std::condition_variable, but when I try to lock the unique_lock associated with it from a second thread I get an exception "Resource deadlock avoided". The thread that created it can lock and unlock it, but not the second…
realh
  • 962
  • 2
  • 7
  • 22
7
votes
1 answer

Spurious wakeup with atomics and condition_variables

std::atomic and std::condition_variable both have member wait and notify_one functions. In some applications, programmers may have a choice between using either for synchronization purposes. One goal with these wait functions is that they should…
Mark Wallace
  • 528
  • 2
  • 12
7
votes
1 answer

Is there a way to atomically flush a binary semaphore in C++ on Linux?

Some kernels provide a "flush" operation on semaphore to unblock all tasks waiting on a semaphore. For example, VxWorks has a semFlush() API that atomically unblocks all tasks pended on a specified semaphore, i.e., all tasks will be unblocked before…
7
votes
3 answers

How can you implement a condition variable using semaphores?

A while back I was thinking about how to implement various synchronization primitives in terms of one another. For example, in pthreads you get mutexes and condition variables, and from these can build semaphores. In the Windows API (or at least,…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
7
votes
5 answers

thread sync using mutex and condition variable

I'm trying to implement an multi-thread job, a producer and a consumer, and basically what I want to do is, when consumer finishes the data, it notifies the producer so that producer provides new data. The tricky part is, in my current impl,…
avocado
  • 2,615
  • 3
  • 24
  • 43
7
votes
2 answers

Using std::conditional_variable to wait on a condition

For simplicity, let's assume that we have only one conditional variable to match a single condition that is reflected by a boolean. 1) Why does std::condition_variable::wait(...) locks the mutex again after a "notify" has been sent to un-sleep…
Justin Bieber
  • 301
  • 1
  • 3
  • 8
7
votes
2 answers

std::condition_variable why does it need a std::mutex

I am not sure if I really understand why std::condition_variable needs a additional std::mutex as a parameter? Should it not be locking by its self? #include #include #include #include…
Pascal
  • 2,175
  • 4
  • 39
  • 57
7
votes
1 answer

Why is there no wait function for condition_variable which does not relock the mutex

Consider the following example. std::mutex mtx; std::condition_variable cv; void f() { { std::unique_lock lock( mtx ); cv.wait( lock ); // 1 } std::cout << "f()\n"; } void g() { std::this_thread::sleep_for( 1s ); …
Claas Bontus
  • 1,628
  • 1
  • 15
  • 29