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
207
votes
10 answers

Why do pthreads’ condition variable functions require a mutex?

I’m reading up on pthread.h; the condition variable related functions (like pthread_cond_wait(3)) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
111
votes
7 answers

Do I have to acquire lock before calling condition_variable.notify_one()?

I am a bit confused about the use of std::condition_variable. I understand I have to create a unique_lock on a mutex before calling condition_variable.wait(). What I cannot find is whether I should also acquire a unique lock before calling…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
95
votes
3 answers

Calling pthread_cond_signal without locking mutex

I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutex after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It…
B Faley
  • 17,120
  • 43
  • 133
  • 223
90
votes
9 answers

C++11 thread-safe queue

A project I'm working on uses multiple threads to do work on a collection of files. Each thread can add files to the list of files to be processed, so I put together (what I thought was) a thread-safe queue. Relevant portions follow: // qMutex is a…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
81
votes
8 answers

When is a condition variable needed, isn't a mutex enough?

I'm sure mutex isn't enough that's the reason the concept of condition variables exist; but it beats me and I'm not able to convince myself with a concrete scenario when a condition variable is essential. Differences between Conditional variables,…
76
votes
3 answers

understanding of pthread_cond_wait() and pthread_cond_signal()

Generally speaking, pthread_cond_wait() and pthread_cond_signal() are called as below: //thread 1: pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); do_something() pthread_mutex_unlock(&mutex); //thread…
user1944267
  • 1,557
  • 5
  • 20
  • 27
69
votes
2 answers

threading.Condition vs threading.Event

I have yet to find a clear explanation of the differences between Condition and Event classes in the threading module. Is there a clear use case where one would be more helpful than the other? All the examples I can find use a producer-consumer…
Parker Ault
  • 3,268
  • 3
  • 22
  • 24
65
votes
3 answers

Differences between Conditional variables, Mutexes and Locks

For example the c++0x interfaces I am having a hard time figuring out when to use which of these things (cv, mutex and lock). Can anyone please explain or point to a resource? Thanks in advance.
Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
58
votes
1 answer

What's the difference between notify_all() and notify_one() of std::condition_variable?

Currently, I am implementing a multi-thread project using std::thread in C++11. I use std::condition_variable to synchronize threads. In detail, one consumer function calls wait() member function of std::condition_variable to wait for task from a…
Yun Huang
  • 4,256
  • 7
  • 27
  • 36
45
votes
3 answers

What is the best way to wait on multiple condition variables in C++11?

First a little context: I'm in the process of learning about threading in C++11 and for this purpose, I'm trying to build a small actor class, essentially (I left the exception handling and propagation stuff out) like so: class actor { private:…
Julian
  • 1,472
  • 1
  • 11
  • 19
40
votes
4 answers

What is the difference between std::condition_variable::wait_for and std::condition_variable::wait_until?

The reference I'm using explains the two in the following way: wait_for "blocks the current thread until the condition variable is woken up or after the specified timeout duration" wait_until "blocks the current thread until the condition variable…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
39
votes
6 answers

Why does Python threading.Condition() notify() require a lock?

My question refers specifically to why it was designed that way, due to the unnecessary performance implication. When thread T1 has this code: cv.acquire() cv.wait() cv.release() and thread T2 has this code: cv.acquire() cv.notify() # requires…
37
votes
1 answer

Compilation error : 'this' cannot be implicitly captured in this context

I am trying to add a condition_variable to handle threads, but get a compilation error at this line: this->cv.wait(lk, []{return this->ready;}); Looks like the for the variable this->ready, the this is not in the right scope. In Java this can be…
Ray
  • 16,025
  • 5
  • 31
  • 51
36
votes
2 answers

Stopping C++ 11 std::threads waiting on a std::condition_variable

I am trying to understand the basic multithreading mechanisms in the new C++ 11 standard. The most basic example I can think of is the following: A producer and a consumer are implemented in separate threads The producer places a certain amount of…
Devon Cornwall
  • 957
  • 1
  • 7
  • 17
28
votes
5 answers

condition variable - why calling pthread_cond_signal() before calling pthread_cond_wait() is a logical error?

It's written in POSIX threads tutorial https://computing.llnl.gov/tutorials/pthreads/ that it is a logical error. my question is why it is a logical error? In my program i need to use these signals, however i cannot guarantee that there will be a…
Asher Saban
  • 4,673
  • 13
  • 47
  • 60
1
2 3
47 48