condition-variable

Stopping C++ 11 std::threads waiting on a std::condition_variable

浪尽此生 提交于 2019-12-03 06:36:32
问题 I am trying to understand the basic multithreading mechanisms in the new C++ 11 standard. The most basic example I can think of is the following: A producer and a consumer are implemented in separate threads The producer places a certain amount of items inside a queue The consumer takes items from the queue if there are any present This example is also used in many school books about multithreading and everything about the communication process works fine. However, I have a problem when it

threading.Condition vs threading.Event

佐手、 提交于 2019-12-03 04:32:41
问题 I have yet to find a clear explanation of the differences between Condition and Event classes in the threading module. Is there a clear use case where one would be more helpful than the other? All the examples I can find use a producer-consumer model as an example, where queue.Queue would be the more straightforward solution. 回答1: Simply put, you use a Condition when threads are interested in waiting for something to become true, and once its true, to have exclusive access to some shared

Stopping C++ 11 std::threads waiting on a std::condition_variable

ε祈祈猫儿з 提交于 2019-12-02 20:16:54
I am trying to understand the basic multithreading mechanisms in the new C++ 11 standard. The most basic example I can think of is the following: A producer and a consumer are implemented in separate threads The producer places a certain amount of items inside a queue The consumer takes items from the queue if there are any present This example is also used in many school books about multithreading and everything about the communication process works fine. However, I have a problem when it comes to stopping the consumer thread. I want the consumer to run until it gets an explicit stop signal

threading.Condition vs threading.Event

有些话、适合烂在心里 提交于 2019-12-02 17:02:16
I have yet to find a clear explanation of the differences between Condition and Event classes in the threading module. Is there a clear use case where one would be more helpful than the other? All the examples I can find use a producer-consumer model as an example, where queue.Queue would be the more straightforward solution. donkopotamus Simply put, you use a Condition when threads are interested in waiting for something to become true, and once its true, to have exclusive access to some shared resource. Whereas you use an Event when threads are just interested in waiting for something to

Mutex status after spurious wakeup

本小妞迷上赌 提交于 2019-12-02 01:47:31
问题 Consider this basic multithreading program using pthreads. We have a main thread, creating another thread that does some work. bool done = false; mutex m; condition c; void foo() { pthread_mutex_lock(&m); //while(!done) { pthread_cond_wait(&c, &m); // Spuriously wakeup while child is doing work. // child thread has NOT unlocked the mutex yet // Do I now own the mutex? // or am I waiting for child to unlock it? //} pthread_mutex_unlock(&m); } void * child(void *arg) { pthread_mutex_lock(&m);

Android: C++ thread not waking up if screen-locked or in background. Works fine when app is in use

风流意气都作罢 提交于 2019-12-01 19:40:10
In our Android app, we have UI component and core C++11 module. A thread is running based on std::chrono::system_clock::time_point , such as below: while(this->m_ConditionVariable.wait_until(lock, this->m_Object.to_time_point()) == std::cv_status::no_timeout) { // ... handle any notify() or arbitrary sleep breaks } Execute(); // <--- not being called consistently Now, we are testing with 1 minute time_point . If the app is in use, then the Execute() is invoked as expected. However, if the app is moved to background or if even the screen is locked, then the Execute() -s behavior is not

Android: C++ thread not waking up if screen-locked or in background. Works fine when app is in use

China☆狼群 提交于 2019-12-01 19:35:55
问题 In our Android app, we have UI component and core C++11 module. A thread is running based on std::chrono::system_clock::time_point , such as below: while(this->m_ConditionVariable.wait_until(lock, this->m_Object.to_time_point()) == std::cv_status::no_timeout) { // ... handle any notify() or arbitrary sleep breaks } Execute(); // <--- not being called consistently Now, we are testing with 1 minute time_point . If the app is in use, then the Execute() is invoked as expected. However, if the app

Can it be assumed that `pthread_cond_signal` will wake the signaled thread atomically with regards to the mutex bond to the condition variable?

主宰稳场 提交于 2019-12-01 14:43:07
Quoting POSIX : The pthread_cond_broadcast() or pthread_cond_signal() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal() . "If predictable scheduling behavior is required". This could/would hint that locking the mutex bound to the condition variable right before calling pthread

std::condition_variable::notify_one() called several times without context switching

拜拜、爱过 提交于 2019-12-01 11:58:41
How many waiting threads will wake up in this example: 1st thread : void wakeUp2Threads() { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.notify_one(); condvar.notify_one(); } 2nd thread : { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.wait(lock); <- 2nd thread has entered here before 1st thread entered wakeUp2Threads. } 3rd thread (the same as 2nd): { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.wait(lock); <- 3rd thread has entered here before 1st thread entered wakeUp2Threads. } Is there any guarantee that in this example both notifications will be

std::condition_variable::notify_one() called several times without context switching

瘦欲@ 提交于 2019-12-01 11:21:40
问题 How many waiting threads will wake up in this example: 1st thread : void wakeUp2Threads() { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.notify_one(); condvar.notify_one(); } 2nd thread : { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.wait(lock); <- 2nd thread has entered here before 1st thread entered wakeUp2Threads. } 3rd thread (the same as 2nd): { std::unique_lock<std::mutex> lock(condvar_mutex); condvar.wait(lock); <- 3rd thread has entered here before 1st