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

When is a mutex prefarable to a condition variable?

I discovered a mutex starvation problem and the propose answer is to use condition variables instead int main () { std::mutex m; std::condition_variable cv; std::thread t ([&] () { while (true) { …
spraff
  • 32,570
  • 22
  • 121
  • 229
-1
votes
1 answer

condition variables error in mutex locks

Okay so in the code below there at least two major problems which should be corrected. #define COUNT_LIMIT 12 pthread_mutex_t c_mutex; pthread_cond_t cond_cv; int count=0; void *inc_count(void *param) { int i=0; for (i=0;i<14;i++) { …
TheRealRave
  • 373
  • 1
  • 8
  • 21
-1
votes
1 answer

Python lock: Should I put time-cost tasks inside a lock or a conditional variable?

should_go = False cv = Condition() while True: with cv: if not should_go: cv.wait() if should_go: # process_time_cost_tasks() should_go = False def request(): with cv: should_go =…
Bob
  • 105
  • 1
  • 12
-1
votes
1 answer

Condition variable - unexpected behaviour

I'm trying to make my program to do this: Take input: nrNodes NrWorkers 3 threads(workers) can only access at a moment the list(read), but only 1 can write. when 5 nodes have been done(sqrt value), it should stop and let in the cleaner thread which…
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
-2
votes
1 answer

C/C++ multithreading - run threads sequentially, sorted based on a particular variable

I am running a multithreaded application in C++. The number of threads is variable and each thread has it's own set of variables, parsed from a JSON type command line argument. I have to run these threads sequentially (may defeat the purpose of…
bvj0412
  • 17
  • 5
-2
votes
1 answer

std::condition_variable - Is there any difference in performance?

Is there any difference in performance between those two: First version: std::condition_variable conditionVarible; std::mutex mutex; std::chrono::steady_clock::time_point timePoint; timePoint = std::chrono::steady_clock::now() +…
Kasata Ata
  • 365
  • 3
  • 11
-3
votes
1 answer

C++ thread and mutex and condition variable

findsmallest common multiple of 10-million numbers in the queue does not exceed 10,000 I killed 2 days to sort out but I just do not understand! please help me #include #include #include
andrew
  • 1
  • 1
1 2 3
47
48