condition-variable

Control each thread by it's own condition variable c++

假如想象 提交于 2019-12-12 01:07:54
问题 I am trying to create a program where there is one master thread and multiple worker threads. Worker threads will register themselves in a shared queue and will wait for a signal from master thread to move on. Master thread will check front of queue and will signal that thread by using it's own specific condition variable, to move on. Here is the pseudo-code that i have right now, struct condition{ pthread_cond_t cond_var; pthread_mutex_t lock; }; queue<condition> queue; pthread_mutex_t lock;

std::condition_variable only works while debugging?

梦想与她 提交于 2019-12-11 10:51:18
问题 I wrote a project in C++14, compiled with Intel's icpc on Fedora 26 (gcc 7 dependency). Everything was good and working, until I moved my project onto Centos 7, and began experiencing very mysterious behavior. On Centos (scl enable devtoolset-7 bash), source code compiles and links without error, but the project only works while debugging with GDB. Without debugging, the condition variable's notification to wake up a sleeping thread don't function. Multiple worker threads go to sleep, but

why is std::condition_variable::notify_one blocking?

寵の児 提交于 2019-12-11 02:26:00
问题 For some reason the call signal.notify_one() blocks the current thread and doesn't return. I have never heard about this behavior and I don't know how to resolve it. { std::lock_guard<std::mutex> lock(_mutex); _exit = true; // _exit is a std::atomic<bool> } std::cout << "before" << std::endl; _signal.notify_one(); std::cout << "after" << std::endl; _thread.join(); I'm using Microsoft Visual C++ 2015 and the code above is called during destruction. I hope you can point me in the right

signal on condition variable without holding lock

蓝咒 提交于 2019-12-10 14:56:38
问题 So I just found out that it's legal to signal a condition variable if you're not holding the lock in c++11. That seems to open the door to some nasty race condition: std::mutex m_mutex; std::condition_variable m_cv; T1: std::unique_lock<std::mutex> lock(m_mutex); m_cv.wait(lock, []{ return !is_empty(); }); T2: generate_data(); m_cv.notify(); Is it guaranteed that T1 will never end up in a situation where we check is_empty() first (it returning true), then getting preempted by T2 which creates

Signalling a condition variable (pthreads)

风流意气都作罢 提交于 2019-12-10 14:21:35
问题 Suppose some condition variable "cond" is associated with a mutex variable "mutex". If a thread is sleeping on cond after calling pthread_cond_wait(&cond,&mutex) , and another thread that has mutex locked is finished, does it matter whether that thread calls pthread_cond_signal(&cond) before or after calling pthread_mutex_unlock(&mutex) ? Does it even need to unlock the mutex at all if it calls pthread_cond_signal(&cond) , since the sleeping thread will acquire the mutex anyway? EDIT:

C - Guarantee condvars are ready for signalling

天大地大妈咪最大 提交于 2019-12-10 11:45:40
问题 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

Which OS / platforms implement wait morphing optimization?

霸气de小男生 提交于 2019-12-10 06:08:19
问题 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 locked or not. A typical recommendation is to signal while holding the lock unless profiling shows a substantial performance improvement overhead from unlocking (by removing an extra context switch). IIUC, the only disadvantage of holding the lock while signalling is the extra two context switches; the

C++11 Can I ensure a condition_variable.wait() won't miss a notification?

瘦欲@ 提交于 2019-12-09 17:58:40
问题 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(); Unfortunately I'm hitting a timing issue: T1: condition checks true T2: condition set to false T2: cv.notify_one() T1: cv.wait() Thread 1 misses the notification completely and remains blocked on wait(). I tried using the version of wait() which takes a predicate but with essentially the same result. That is, the

Do condition variables still need a mutex if you're changing the checked value atomically?

非 Y 不嫁゛ 提交于 2019-12-09 04:39:01
问题 Here is the typical way to use a condition variable: // The reader(s) lock(some_mutex); if(protected_by_mutex_var != desired_value) some_condition.wait(some_mutex); unlock(some_mutex); // The writer lock(some_mutex); protected_by_mutex_var = desired_value; unlock(some_mutex); some_condition.notify_all(); But if protected_by_mutex_var is set atomically by say, a compare-and-swap instruction, does the mutex serve any purpose (other than that pthreads and other APIs require you to pass in a

How can you implement a condition variable using semaphores?

雨燕双飞 提交于 2019-12-08 17:37:57
问题 A while back I was thinking about how to implement various synchronization primitives in terms of one another. For example, in pthreads you get mutexes and condition variables, and from these can build semaphores. In the Windows API (or at least, older versions of the Windows API) there are mutexes and semaphores, but no condition variables. I think that it should be possible to build condition variables out of mutexes and semaphores, but for the life of me I just can't think of a way to do