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

Is possible that a pthread_cond_wait() consumes multiple pthread_cond_signal()?

I've tested this scenario in some environments, and I got the following flow: However, from the man pages ( http://linux.die.net/man/3/pthread_cond_wait ) or ( http://linux.die.net/man/3/pthread_cond_signal ), I cannot find any guarantee that the…
Pacheco
  • 133
  • 7
7
votes
2 answers

Why do condition variables sometimes erroneously wake up?

I've known for eons that the way you use a condition variable is lock while not task_done wait on condition variable unlock Because sometimes condition variables will spontaneously wake. But I've never understood why that's the case. In the…
aspo
  • 146
  • 4
7
votes
2 answers

predicate for condition variable

I am new to multi threading. While writing multi threaded code in C++11 using condition variable , I use the following construct while(predicate) { cond_var.wait(&lock); } However, I have been reading Deitel's third edition book on operating…
claudius
  • 1,112
  • 1
  • 10
  • 23
7
votes
2 answers

C++11 Can I ensure a condition_variable.wait() won't miss a notification?

I have thread 1 executing the following code: unique_lock ul(m); while(condition == true) cv.wait(ul); And thread 2 executing this code: condition = false; cv.notify_one(); Unfortunately I'm hitting a timing issue: T1: condition checks…
screwnut
  • 1,367
  • 7
  • 26
7
votes
1 answer

Control multithreaded flow with condition_variable

I haven't wrapped my head around the C++11 multithreading stuff yet, but I'm trying to have multiple threads wait until some event on the main thread and then all continue at once (processing what happened), and wait again when they're done…
David
  • 27,652
  • 18
  • 89
  • 138
6
votes
3 answers

When can a cond var be used to synchronize its own destruction/unmapping?

According to POSIX, It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked. Further, the signal and broadcast operations are specified to unblock one/all threads blocked on the condition…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
6
votes
3 answers

CONDITION_VARIABLE in windows; wont compile

I am trying to make a windows-version of a program written for Linux, in C++. For the program to be thread-safe, I use pthread_cond_t and pthread_cond_wait in the Linux version. These functions use a mutex to help make sure that the waiting thread…
pjaall
  • 103
  • 1
  • 5
6
votes
1 answer

condition variable

What are the principles of a condition variable in synchronization of the processes of operating systems?
likeIT
  • 297
  • 5
  • 10
6
votes
2 answers

Windows Condition Variable vs. Event

We can use either the new condition variable primitive or windows event in order to synchronize threads in WinNT v6.x or later. Consider the following two approaches, we want workers to run at the same time when "go" is set in main, otherwise they…
Y.Z
  • 626
  • 2
  • 9
  • 21
6
votes
2 answers

Signalling a condition variable (pthreads)

Suppose some condition variable "cond" is associated with a mutex variable "mutex". If a thread is sleeping on cond after calling pthread_cond_wait(&cond,&mutex), and another thread that has mutex locked is finished, does it matter whether that…
ManRow
  • 1,563
  • 4
  • 21
  • 40
6
votes
2 answers

Please explain the use of condition variables in c++ threads, and why do we need to use `unique_lock` and `mutex` alongwith this

I am refering to this particular piece of code: this code basically has three threads 1. Perform some handshaking with server 2. Load Data from XML files. 3. Do processing on data loaded from XML. As we can see that Task 1 is not dependent on any…
Nilesh Kumar
  • 343
  • 1
  • 5
  • 18
6
votes
0 answers

Is this usage of condition variable wrong?

In the chapter 6 of C++ Concurrency in Action, it implements a thread-safe queue. Here is the complete code. But I find there may be something wrong with its use of condition variable. std::unique_lock wait_for_data() { …
olist
  • 877
  • 5
  • 13
6
votes
1 answer

Possible race condition in std::condition_variable?

I've looked into the VC++ implementation of std::condition_variable(lock,pred), basically, it looks like this: template void wait(unique_lock& _Lck, _Predicate _Pred) { // wait for signal and test…
David Haim
  • 25,446
  • 3
  • 44
  • 78
6
votes
1 answer

What is the fastest race free method for polling a lockless queue?

Say we have a single-producer-thread single-consumer-thread lockless queue, and that the producer may go long periods without producing any data. It would be beneficial to let the consumer thread sleep when there is nothing in the queue (for the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
6
votes
2 answers

Should I use condition variables from the C++ standard or from the Windows API?

When implementing condition variables into a Win32 C++ program, would it be better to use Win32 functions, classes, and data types (e.g. CreateThread, SleepConditionVariableCS, WaitForSingleObjectEx, ReleaseMutex, CONDITION_VARIABLE) or those from…
Cerran
  • 2,087
  • 2
  • 21
  • 33