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

Using condition variable in a producer-consumer situation

I'm trying to learn about condition variables and how to use it in a producer-consumer situation. I have a queue where one thread pushes numbers into the queue while another thread popping numbers from the queue. I want to use the condition variable…
jasonline
  • 8,646
  • 19
  • 59
  • 80
27
votes
3 answers

Share condition variable & mutex between processes: does mutex have to locked before?

I need to some little help to understand how to use condition variables in C to resolve an exercise. Here is a little example: #include #include #include #include #include #include…
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
27
votes
2 answers

what if notify() is called before wait()?

I have a situation where a notify() 'can' be called before a wait(). I am trying to make a simulator to schedule its next event when I 'notify' him by sending him messages. So I have devised a wait->notify->scedule chain void Broker::pause() { …
rahman
  • 4,820
  • 16
  • 52
  • 86
26
votes
2 answers

What is the difference between std::condition_variable and std::condition_variable_any?

I'm probably missing something obvious, but I can't see any difference between std::condition_variable and std::condition_variable_any. Why do we need both?
John Gordon
  • 2,576
  • 3
  • 24
  • 29
23
votes
4 answers

Event notification without mutex

C++11 has the std::condition_variable, its wait function is template< class Predicate > void wait( std::unique_lock& lock, Predicate pred ); It requires a mutex. As far as I understand - its notify_one can be called without…
qble
  • 1,256
  • 2
  • 12
  • 29
21
votes
3 answers

How to use a condition_variable to really wait_for no longer than a certain duration

As it turns out, condition_variable::wait_for should really be called condition_variable::wait_for_or_possibly_indefinitely_longer_than, because it needs to reacquire the lock before really timing out and returning. See this program for a…
Julian
  • 4,170
  • 4
  • 20
  • 27
20
votes
2 answers

Difference between std::atomic and std::condition_variable wait, notify_* methods

I was looking through 'Atomic operations library' and came across a new c++20 feature of atomic 'wait' and 'notify_' methods. I am curious on what the differences are in regards to std::condition_variable's 'wait' and 'notify_' methods.
PVRT
  • 480
  • 4
  • 13
20
votes
4 answers

How do I deal with the system clock changing while waiting on a std::condition_variable?

I'm trying to implement some cross-platform code in C++11. Part of this code implements a semaphore object using a std::condition_variable. When I need to do a timed wait on the semaphore, I use wait_until or wait_for. The problem I'm experiencing…
jldeon
  • 363
  • 2
  • 11
18
votes
1 answer

Usage example of boost::condition::timed_wait

Does someone have an example of how to most easily use boost::condition::timed_wait? There are some threads on the topic here, here and here, but none feature a working example. And boost doc is as usual quite sparse.
Cookie
  • 12,004
  • 13
  • 54
  • 83
18
votes
8 answers

How to wake a std::thread while it is sleeping

I am using C++11 and I have a std::thread which is a class member, and it sends information to listeners every 2 minutes. Other that that it just sleeps. So, I have made it sleep for 2 minutes, then send the required info, and then sleep for 2…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
16
votes
4 answers

c++ should condition variable be notified under lock

I found the following example for condition variable on www.cppreference.com, http://en.cppreference.com/w/cpp/thread/condition_variable. The call to cv.notify_one() is placed outside the lock. My question is if the call should be made while holding…
Nemo
  • 3,285
  • 1
  • 28
  • 22
16
votes
5 answers

What are common uses of condition variables in C++?

I'm trying to learn about condition variables. I would like to know what are the common situations where condition variables are used. One example is in a blocking queue, where two threads access the queue - the producer thread pushes an item into…
jasonline
  • 8,646
  • 19
  • 59
  • 80
16
votes
4 answers

C++11 std::condition_variable: can we pass our lock directly to the notified thread?

I'm learning about C++11 concurrency, where my only prior experience with concurrency primitives was in Operating Systems class six years ago, so be gentle, if you can. In C++11, we can write std::mutex m; std::condition_variable cv; std::queue
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
16
votes
2 answers

What is the difference between busy-wait and polling?

From the Wikipedia article on Polling Polling, or polled operation, in computer science, refers to actively sampling the status of an external device by a client program as a synchronous activity. Polling is most often used in terms of input/output…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
15
votes
1 answer

Do condition variables still need a mutex if you're changing the checked value atomically?

Here is the typical way to use a condition variable: // The reader(s) lock(some_mutex); if(protected_by_mutex_var != desired_value) some_condition.wait(some_mutex); unlock(some_mutex); // The writer lock(some_mutex); protected_by_mutex_var =…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
1
2
3
47 48