condition-variable

What is the best way to wait on multiple condition variables in C++11?

蓝咒 提交于 2019-12-17 18:16:36
问题 First a little context : I'm in the process of learning about threading in C++11 and for this purpose, I'm trying to build a small actor class, essentially (I left the exception handling and propagation stuff out) like so: class actor { private: std::atomic<bool> stop; private: std::condition_variable interrupt; private: std::thread actor_thread; private: message_queue incoming_msgs; public: actor() : stop(false), actor_thread([&]{ run_actor(); }) {} public: virtual ~actor() { // if the actor

Do I have to acquire lock before calling condition_variable.notify_one()?

 ̄綄美尐妖づ 提交于 2019-12-17 04:50:04
问题 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 also acquire a unique lock before calling notify_one() or notify_all() . Examples on cppreference.com are conflicting. For example, the notify_one page gives this example: #include <iostream> #include <condition_variable> #include <thread> #include <chrono> std::condition_variable cv; std::mutex

Calling pthread_cond_signal without locking mutex

寵の児 提交于 2019-12-17 02:18:31
问题 I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutext after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() routine to complete. My question is: isn't it OK to call pthread_cond_signal or pthread_cond_broadcast methods without locking the mutex? 回答1: If you do

Calling pthread_cond_signal without locking mutex

陌路散爱 提交于 2019-12-17 02:18:20
问题 I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutext after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() routine to complete. My question is: isn't it OK to call pthread_cond_signal or pthread_cond_broadcast methods without locking the mutex? 回答1: If you do

Guaranteed yielding with pthread_cond_wait and pthread_cond_signal

两盒软妹~` 提交于 2019-12-13 15:23:47
问题 Assuming I have a C program with 3 POSIX threads, sharing a global variable, mutex, and condition variable, two of which are executing the following psuedocode: ...process data... pthread_mutex_lock( &mutex ); variable = data_ptr; pthread_cond_signal( &cond ); pthread_mutex_unlock( &mutex ); And the third running: while(1) { while( variable == NULL ) { pthread_mutex_wait( &cond, &mutex ); } printf( "Data is %d", *variable ); } Is it safe to assume that the third thread will see the data from

ConditionVariable prevents both threads from running simultaneously

Deadly 提交于 2019-12-12 22:30:39
问题 I am trying to enforce synchronization between a pair of Android threads for game programming purposes. I have assigned a game thread, which handles most duties, and a render thread, which is tasked with swapping buffers and rendering. When I first asked about thread synchronization, I was referred to the ConditionVariable object as a useful tool to force threads to block until concurrent tasks are completed. My source code looks like this: ... final ConditionVariable bufferLock = new

Multiple vs single condition variable

橙三吉。 提交于 2019-12-12 19:19:45
问题 I'm working on an application where I currently have only one condition variable and lots of wait statements with different conditions. Whenever one of the queues or some other state is changed I just call cv.notify_all(); and know that all threads waiting for this state change will be notified (and possibly other threads which are waiting for a different condition). I'm wondering whether it makes sense to use separate condition variables for each queue or state. All my queues are usually

unlock the mutex after condition_variable::notify_all() or before?

我与影子孤独终老i 提交于 2019-12-12 19:08:29
问题 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: //prepare data for several worker-threads; //and now, awaken the threads: std::unique_lock<std::mutex> lock2(sharedMutex); _threadsCanAwaken = true; lock2.unlock(); _conditionVar.notify_all(); //awaken all the worker threads; //wait until all threads completed; //cleanup: _threadsCanAwaken = false

Why is a condition_variable not MoveAssignable

隐身守侯 提交于 2019-12-12 12:46:52
问题 Why is a condition_variable not MoveConstructible (as per http://en.cppreference.com/w/cpp/thread/condition_variable)? That prohibits inclusion in lots of containers (eg. std::unordered_map ) that move things around. This forces people to use a unique_ptr which incurs one extra heap allocation, which things like make_shared were built to solve. Also if one doesn't have a pool allocator this can become quite inefficient. 回答1: condition_variable is a synchronization construct that multiple

Error compile “CONDITION_VARIABLE undeclared”

孤街浪徒 提交于 2019-12-12 02:48:35
问题 sorry for my bad english if it's bad. my code #include <windows.h> int main(void) { CONDITION_VARIABLE cond; return 0; } i have the following error when i compil. CONDITION_VARIABLE undeclared i have the same error than in this stackoverflow question, and i have seen in my Winbase.h file than i don't have the following typedef typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE; i have found where i can to find a better Winbase.h, i have found this site http://source