condition-variable

c++ condition variable notification not working as expected

∥☆過路亽.° 提交于 2019-12-08 15:28:51
问题 I'm trying to launch new threads as soon as work in previous worker_thread has started , but maybe ended or not. I've replaced started and ended work with time delays. My code is: #include <iostream> #include <string> #include <mutex> #include <condition_variable> #include <future> #include <atomic> #include <chrono> #include <thread> std::mutex m; std::condition_variable cv; bool started = false; void worker_thread() { std::unique_lock<std::mutex> lk(m); static std::atomic<int> count(1); std

C - Guarantee condvars are ready for signalling

时光毁灭记忆、已成空白 提交于 2019-12-08 06:19:25
I have a simple application that interfaces with different pieces of hardware. For each piece of hardware, I spawn a pthread_t against a unique monitor function, for a total of 6 threads: 1 manager thread, and 5 worker threads. Each thread has a common initialization routine where it waits for the manager thread to wake it up via: pthread_mutex_lock(&mMutex); pthread_cond_wait(&mMutex, &condVar); pthread_mutex_lock(&mMutex); The main thread then wakes up all of the threads by signalling them one at a time: pthread_cond_wait(&mMutex1, &condVar1); pthread_cond_wait(&mMutex2, &condVar2); ...

Why does boost's interprocess_condition deadlock in notify_one?

南笙酒味 提交于 2019-12-08 03:59:47
问题 This is a link to an MCVE that demonstrates the deadlock It contains five parts: SharedEvent is an implementation of an AutoResetEvent that is stored in shared memory. CreatedSharedEvent creates a named shared memory object in which a SharedEvent is allocated. It provides an accessor method that returns a reference to the SharedEvent. OpenedSharedEvent opens a named shared memory object in which a SharedEvent has already been allocated. It also provides an accessor method that returns a

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

情到浓时终转凉″ 提交于 2019-12-07 13:02:59
问题 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 other Tasks but Task 3 is dependent on Task 2. So, it means Task 1 and Task 2 can be run in parallel by different Threads to improve the performance of application. Thus, application is built to be multithreaded. #include <iostream> #include <thread>

Using std::conditional_variable to wait on a condition

£可爱£侵袭症+ 提交于 2019-12-07 03:38:35
问题 For simplicity, let's assume that we have only one conditional variable to match a single condition that is reflected by a boolean. 1) Why does std::condition_variable::wait(...) locks the mutex again after a "notify" has been sent to un-sleep it? 2) Seeing the behaviour in "1)", does that mean that when you do std::condition_variable::notify_all it only makes it so that all of the waiting threads are unblocked/woken up... but in order instead of all at once? If so, what can be done to do it

c++ should condition variable be notified under lock

孤街醉人 提交于 2019-12-07 01:50:09
问题 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 the lock to guarantee that waiting threads are in fact in waiting state and will receive the notify signal. #include <iostream> #include <string> #include <thread> #include <mutex> #include <condition_variable> std::mutex m; std::condition

Control multithreaded flow with condition_variable

≡放荡痞女 提交于 2019-12-07 00:51:57
问题 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 processing... looping until they're shut down. Below isn't exactly that - it's a simpler reproduction of my problem: std::mutex mutex; std::condition_variable cv; std::thread thread1([&](){ std::unique_lock<std::mutex> lock(mutex); cv.wait(lock); std::cout

Using a lock with condition variables

本小妞迷上赌 提交于 2019-12-06 06:20:16
问题 Consider the following simplistic example of condition variables: bool pause = true; boost::mutex::scoped_lock lock(m_mutex); while (!pause) cv.wait(lock); and boost::mutex::scoped_lock lock(m_mutex); pause = false; cv.notify_one(); Do we essentially need the scoped_lock or any other lock here, if we are running the code on a processor which supports updates on byte-granularity. This essentially means that the assignment of bools are atomic which is typically the case with x86 processors. Has

Possible race condition in std::condition_variable?

时光毁灭记忆、已成空白 提交于 2019-12-06 02:49:40
问题 I've looked into the VC++ implementation of std::condition_variable(lock,pred) , basically, it looks like this: template<class _Predicate> void wait(unique_lock<mutex>& _Lck, _Predicate _Pred) { // wait for signal and test predicate while (!_Pred()) wait(_Lck); } Basically , the naked wait calls _Cnd_waitX which calls _Cnd_wait which calls do_wait which calls cond->_get_cv()->wait(cs); (all of these are in the file cond.c). cond->_get_cv() returns Concurrency::details::stl_condition_variable

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

℡╲_俬逩灬. 提交于 2019-12-06 01:37:07
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 other Tasks but Task 3 is dependent on Task 2. So, it means Task 1 and Task 2 can be run in parallel by different Threads to improve the performance of application. Thus, application is built to be multithreaded. #include <iostream> #include <thread> #include <functional> #include <mutex> #include <condition_variable> using namespace std::placeholders