condition-variable Questions

7

Solved

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-s...
Shadchan asked 7/3, 2013 at 17:56

3

Solved

I have thread 1 executing the following code: unique_lock<mutex> ul(m); while(condition == true) cv.wait(ul); And thread 2 executing this code: condition = false; cv.notify_one(); Unfo...
Groupie asked 29/7, 2012 at 2:36

2

Condition variables should have have a single order with respect to notify() and unlock_sleep() (an imaginary function call used within wait() where the mutex is unlocked and the thread sleeps as o...

6

Solved

Looking at several videos and the documentation example, we unlock the mutex before calling the notify_all(). Will it be better to instead call it after? The common way: Inside the Notifier thread:...
Lux asked 25/9, 2018 at 17:1

8

Solved

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 ess...

1

Solved

Take this code: std::condition_variable var; var.wait(lock, [&sharedBool] { return sharedBool; }); When var reads from sharedBool, is that thread safe? If it isn't, is making sharedBool a std:...
Leslee asked 30/5, 2023 at 18:56

4

Solved

C++11 has the std::condition_variable, its wait function is template< class Predicate > void wait( std::unique_lock<std::mutex>& lock, Predicate pred ); It requires a mutex. As ...
Fredricfredrick asked 8/4, 2013 at 14:47

4

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 ...
Principal asked 3/3, 2016 at 14:55

1

Signaling between threads can be achieved with std::promise/std::future or with good old condition variables. Can someone provide examples/use case where one would be a better choice over the other...
Quasijudicial asked 20/10, 2017 at 18:42

1

I'm reading some example code of condition_variable: At cppreference, the notify_one() is called like this: https://en.cppreference.com/w/cpp/thread/condition_variable { std::lock_guard lk(m); re...
Breann asked 30/8, 2022 at 13:55

5

Solved

Spurious wakeup is allowed by various platforms. To counter that, we write below looping mechanism: while(ContinueWaiting()) cv.wait(lock); // cv is a `std::conditional_variable` object Same thin...

7

Solved

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...
Coneflower asked 14/6, 2013 at 5:51

1

Solved

Which major OS / platforms implement wait morphing? This question came up when I noticed that there's no clearcut best practice about whether one should signal a condition variable with mutex lock...

1

Solved

std::atomic<T> and std::condition_variable both have member wait and notify_one functions. In some applications, programmers may have a choice between using either for synchronization purpose...
Dacy asked 11/5, 2022 at 3:11

2

I'm writing a C++ ThreadPool implantation and using pthread_cond_wait in my worker's main function. I was wondering how much time will pass from signaling the condition variable until the thread/th...
Barracks asked 20/8, 2017 at 15:23

2

Solved

#include <iostream> #include <thread> #include <mutex> #include <atomic> using namespace std; const int FLAG1 = 1, FLAG2 = 2, FLAG3 = 3; int res = 0; atomic<int> flag...
Shaffer asked 31/12, 2021 at 13:1

6

TL;DR Why does std::condition_variable::wait needs a mutex as one of its variables? Answer 1 You may look a the documentation and quote that: wait... Atomically releases lock But that's no...
Woodrowwoodruff asked 7/9, 2017 at 5:18

2

Solved

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....
Personally asked 14/5, 2018 at 13:10

2

Solved

I am working with condition_variable on Visual studio 2019. The condition_variable.wait_for() function returns std::cv_status::no_timeout without any notification. #include <iostream> #includ...
Chiles asked 20/11, 2020 at 12:15

3

Solved

This is a general question. For example, currently two child threads have called pthread_cond_wait(&cond1,&mutex), and they are both waiting. Then, the parent thread calls pthread_cond_sig...
Spirit asked 18/3, 2013 at 20:31

1

For study purposes, I’m comparing implementations of single producer single consumer queues. So I compared a condition variable implementation with a C++20 counting semaphore implementation. I woul...
Cowage asked 8/12, 2020 at 14:47

6

Solved

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 ...

3

Solved

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_...
Marrowbone asked 13/5, 2013 at 13:7

1

I have a timer class which uses the std::condition_variable wait_until (I have also tried wait_for). I am using the std::chrono::steady_clock time to wait until a specific time in the future. This ...
Nahshu asked 28/8, 2020 at 10:25

2

Solved

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_v...
Airlift asked 12/7, 2020 at 9:56

© 2022 - 2024 — McMap. All rights reserved.