condition-variable

C++11 std::condition_variable: can we pass our lock directly to the notified thread?

一笑奈何 提交于 2019-12-23 06:58:35
问题 I'm learning about C++11 concurrency, where my only prior experience with concurrency primitives was in Operating Systems class six years ago, so be gentle, if you can. In C++11, we can write std::mutex m; std::condition_variable cv; std::queue<int> q; void producer_thread() { std::unique_lock<std::mutex> lock(m); q.push(42); cv.notify_one(); } void consumer_thread() { std::unique_lock<std::mutex> lock(m); while (q.empty()) { cv.wait(lock); } q.pop(); } This works fine, but I'm offended by

Condition Variable - Wait/Notify Race Condition

删除回忆录丶 提交于 2019-12-22 07:58:09
问题 I'll present some code first since explaining is easier that way. Assume that mutexes are correctly used with the condition variables to keep it simple: // Thread 1 while(1) { conditionVariable.wait(); // Do some work } // Thread 2 while(1) { // Do some work conditionVariable.notify_one(); } // Thread 3 while(1) { // Do some work conditionVariable.notify_one(); } What I would like to achieve is that thread 1 is guaranteed to be waiting on the condition variable when thread 2 or Thread 3

Adding blocking functions to lock-free queue

蓝咒 提交于 2019-12-21 20:29:07
问题 I have a lock-free multi producer, single consumer queue, based on a circular buffer. So far, it only has non-blocking push_back() and pop_front() calls. Now I want to add blocking versions of those calls, but I want to minimize the impact this has on the performance of code that uses the non-blocking versions - namely, it should not turn them into " lock-by-default " calls. E.g. the simplest version of a blocking push_back() would look like this: void push_back_Blocking(const T& pkg) { if (

Using channel or sync.Cond to wait for condition

旧街凉风 提交于 2019-12-21 05:47:10
问题 I am trying to wait for a specific condition, and I would like advice as to how this is done best. I have a struct that looks like this (simplified): type view struct { timeFrameReached bool Rows []*sitRow } In a goroutine, I am updating a file, which is read into the view variable. The number of rows increases, and timeFrameReached will ultimately be true . Elsewhere, I want to wait for the following condition to be true: view.timeFrameReached == true || len(view.Rows) >= numRows I am trying

What is the consensus number for semaphores?

一笑奈何 提交于 2019-12-20 09:57:34
问题 (I think that) the consensus number for a mutex is 2. What is the consensus number for semaphores (like in pthread_sem_*)? What is the consensus number for condition variables (like in pthread_cond_*)? 回答1: The consensus number for a mutex would be 1. It's trivially clear that a mutex will be wait-free for a single thread. From its definition, it's also clear that a mutex is no longer wait-free for two threads. The consensus number therefore is >=1 and <2, so it must be 1. Likewise, other

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-19 11:53:37
问题 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".

Why is there no wait function for condition_variable which does not relock the mutex

僤鯓⒐⒋嵵緔 提交于 2019-12-18 04:59:14
问题 Consider the following example. std::mutex mtx; std::condition_variable cv; void f() { { std::unique_lock<std::mutex> lock( mtx ); cv.wait( lock ); // 1 } std::cout << "f()\n"; } void g() { std::this_thread::sleep_for( 1s ); cv.notify_one(); } int main() { std::thread t1{ f }; std::thread t2{ g }; t2.join(); t1.join(); } g() "knows" that f() is waiting in the scenario I would like to discuss. According to cppreference.com there is no need for g() to lock the mutex before calling notify_one .

How To Use Condition Variable

99封情书 提交于 2019-12-17 22:52:21
问题 The Linux Programming Interface book has a piece of code (producer/consumer) to show how condition variable works: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int avail = 0; while (TRUE) { s = pthread_mutex_lock(&mtx); while (avail == 0) { /* Wait for something to consume */ s = pthread_cond_wait(&cond, &mtx); } while (avail > 0) { /* Consume all available units */ avail--; } s = pthread_mutex_unlock(&mtx); } Why we use

Whats the difference between std::condition_variable and std::condition_variable_any?

浪尽此生 提交于 2019-12-17 19:48:22
问题 I'm probably missing something obvious, but I can't see any difference between between std::condition_variable and std::condition_variable_any . Why do we need both? 回答1: std::condition_variable is more specialized, and therefore can be more efficient when you don't need the flexibility of std::condition_variable_any . From N3290 §30.5[thread.condition]/1 Class condition_variable provides a condition variable that can only wait on an object of type unique_lock<mutex> , allowing maximum

condition variable [closed]

丶灬走出姿态 提交于 2019-12-17 18:47:15
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . What are the principles of a condition variable in synchronization of the processes of operating systems? 回答1: Well, conditional variables allow you to wait for certain condition to occur. In practice your thread may sleep on conditional variable and other thread wakes it up.