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
0
votes
2 answers

Significance of signalling a condition variable when no one is waiting?

When reading a book on concurrency, the author says a semaphore is different than a condition variable in the way signal() works. The semaphore keeps track of the number of calls to signal() while the condition variable does not. "Calling…
cody
  • 651
  • 2
  • 8
  • 16
0
votes
2 answers

condition variables signaling

In this link here, in the increment function, condition variable is signalled before actually incrementing the count(from zero). Should the signal be not invoked after incrementing the count? Or does the wait call in decrement_count function not…
Kamal
  • 3,068
  • 5
  • 26
  • 26
0
votes
1 answer

multiple threads but only one allowed to use method

So basically the situation I am in is I have a bunch of threads each doing different calculations throughout the week. At the end of the week, every thread calls function X() and then starts calculating for the next week and repeats this cycle.…
0
votes
3 answers

How do you share a mutex among different instances of a class?

I was wondering how to share a mutex in one class amongst different instances of another class. Right now, I have a class, Indexer, that has a Boost mutex and condition_variable as private member variables. I create an auto_ptr of the Indexer class…
noko
  • 1,129
  • 2
  • 14
  • 25
0
votes
0 answers

Search for a design of single-writer-multi-reader multithreading communication

The application context is as follows: It is a single-process-multi-threads program It is designed for automated-futures-trading. In futures trading market, there are hundreds of active future contracts. Market data of each contract is called…
0
votes
1 answer

an easy explanation of where to and how to use condition variables?

I have searched google for nearly 2 hours to get an idea of what are the situations of using condition variables. But I have found nothing but a bunch of irritating code snippets harder to realize. So I need clear conception regarding this.
Md. Arafat Al Mahmud
  • 3,124
  • 5
  • 35
  • 66
-1
votes
0 answers

Shared memory condition variable block after process crash

I'm running some process with POSIX cond var on shared memory, orig question: boost-ipc-condition-variable-blocks. I run 2, 3 or 4 processes and if one of them stops, the other two get block on the cond var or on the mutex, it get solved only after…
lior.i
  • 573
  • 1
  • 7
  • 20
-1
votes
1 answer

Can std::condition_variable::notify_one and wait_for occurr at the same time?

like my code, and i learn in https://en.cppreference.com/w/cpp/thread/condition_variable, my question is why i remove lock_guard with task++; then result is wait timeout. i learned, wait check task is 0, then unlock and to wait, but before wait,…
-1
votes
1 answer

C++ input/output queue for multiple input source

I have a task that requires listening to multiple data source, and process the data to calculate some statistics (like cumulative sum of all data) for every new record coming in. I am reading this post for a queue with condition variables. I wonder…
AlgRev
  • 146
  • 5
-1
votes
1 answer

pthread_cond_signal does not wake up the thread that waits

I am writing a program which creates a thread that prints 10 numbers. When it prints 5 of them, it waits and it is notifying the main thread and then it continues for the next 5 numbers This is test.c #include #include #include…
PanD22
  • 79
  • 7
-1
votes
1 answer

std::thread not waking up when using notify_one() function even with predicate

I've been trying to write both periodic and non-periodic messages on a serial port using Windows API. The architecture of my code is the following: The main thread starts the following : A "message sending thread" that will be responsible for…
GseoC
  • 1
  • 1
-1
votes
1 answer

Why a waiting thread does not wake up after calling notify?

I am trying to simulate a sensor that outputs data at a certain frame rate while another is waiting to have a data ready and when it is ready it copies it locally and processes it. Sensor sensor(1,1000); Monitor monitor; // Function that…
vicalomen
  • 1
  • 2
-1
votes
2 answers

C++: Condition Variable - Is there a mistake in this youtube demo?

Youtube details I have been browsing youtube to try and develop my understanding of C++ multithread support with mutex and condition variables. I came across this video. Skip to time 6:30 to see what I am currently looking at. (A page of…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
-1
votes
1 answer

'std::system_error', producer consumer C++ concurrency

The Producer/Consumer program below should transfer a character at a time to the buffer and then print it. The program initially runs, but then always fail on the third iteration of the consumer loop. -pthread is included at compile time. The…
A.Simpson
  • 33
  • 8
-1
votes
1 answer

pthread and conditional variables

I am following the tutorial on pthread from here. #include #include #include pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t …
ek187
  • 9
  • 3
1 2 3
47
48