condition-variable

Why do pthreads’ condition variable functions require a mutex?

蹲街弑〆低调 提交于 2020-01-07 01:49:09
问题 I’m reading up on pthread.h ; the condition variable related functions (like pthread_cond_wait(3) ) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed to do? 回答1: It's just the way that condition variables are (or were originally) implemented. The mutex is used to protect the condition variable itself . That's why you need it locked before you do a wait. The wait will "atomically" unlock the

Breaking a condition variable deadlock

戏子无情 提交于 2020-01-04 13:13:08
问题 I have a situation where thread 1 is waiting on a condition variable A, which should be woken up by thread 2. Now thread 2 is waiting on a condition variable B , which should be woken up by thread 1. In the scenario I am using the condition variable, I cannot avoid such a deadlock situation. I detect cycle(deadlock) and terminate one of the threads which is a participant in the deadlock. Now, what I am not sure is how to simply terminate a thread say thread 1 which is waiting on a condition

Breaking a condition variable deadlock

不羁岁月 提交于 2020-01-04 13:12:17
问题 I have a situation where thread 1 is waiting on a condition variable A, which should be woken up by thread 2. Now thread 2 is waiting on a condition variable B , which should be woken up by thread 1. In the scenario I am using the condition variable, I cannot avoid such a deadlock situation. I detect cycle(deadlock) and terminate one of the threads which is a participant in the deadlock. Now, what I am not sure is how to simply terminate a thread say thread 1 which is waiting on a condition

Mutex are needed to protect the Condition Variables

你。 提交于 2020-01-01 06:43:48
问题 As it is said that Mutex are needed to protect the Condition Variables. Is the reference here to the actual condition variable declared as pthread_cond_t OR A normal shared variable count whose values decide the signaling and wait. ? 回答1: is the reference here to the actual condition variable declared as pthread_cond_t or a normal shared variable count whose values decide the signaling and wait? The reference is to both. The mutex makes it so that the shared variable ( count in your question)

C++ - How not to miss multiple notifications from multiple threads?

偶尔善良 提交于 2019-12-31 06:57:27
问题 In my application, many threads notify a waiting thread. Sometimes these notifications are very close to each other in time and the waiting thread misses the notification. Is there any easy way to counter this issue? A small example code is given below. In the code, the task2 notifies the waiting thread but the waiting thread, waitingForWork, miss the notification. #include <condition_variable> #include <iostream> #include <thread> std::mutex mutex_; std::condition_variable condVar; bool

POSIX Threads: Condition Variables - what's the point?

时光总嘲笑我的痴心妄想 提交于 2019-12-29 05:34:09
问题 I've been working with pthreads a fair bit recently and there's one little thing I still don't quite get. I know that condition variables are designed to wait for a specific condition to come true (or be 'signalled'). My question is, how does this differ at all from normal mutexes? From what I understand, aren't condition variables just a mutex with additional logic to unlock another mutex (and lock it again) when the condition becomes true? Psuedocode example: mutex mymutex; condvar mycond;

Application crash due to INVALID PARAMETER after call in std::condition_variable

杀马特。学长 韩版系。学妹 提交于 2019-12-25 01:39:01
问题 I have an application that spawns some 20 threads using _beginthreadex . All of the threads are waiting on a queue to be filled which is a wrapper over the std::queue : class MyQueue .The queue is created as a global variable something like MyQueue processQueue The front function of MyQueue looks something like, std::unique_lock<std::mutex> mlock(mutex_); while (queue_.empty()) { cond_.wait(mlock); } auto item = queue_.front(); queue_.pop(); return item; And push looks like: std::unique_lock

One thread showing interest in another thread (consumer / producer)

冷暖自知 提交于 2019-12-24 12:38:15
问题 I would like to have to possibility to make thread (consumer) express interest in when another thread (producer) makes something. But not all the time. Basically I want to make a one-shot consumer. Ideally the producer through would go merrily about its business until one (or many) consumers signal that they want something, in which case the producer would push some data into a variable and signal that it has done so. The consumer will wait until the variable has become filled. It must also

Alternating routines sharing a mutex

爷,独闯天下 提交于 2019-12-24 00:27:18
问题 I have method a that is invoked repeatedly at some random time, which triggers method b , which is completely executed after some random time and is in it own thread. I want to ensure that a subsequent execution of a waits until b is completed, which is triggered by the current execution of a . In other words, a and b are to be executed alternatively. I tried to do this using mutex and condition variable as follows: def a Thread.new do $mutex.synchronize do puts "a" b $cv.wait($mutex) end end

Can C++11 condition_variables be used to synchronize processes?

南笙酒味 提交于 2019-12-23 20:21:25
问题 I'm trying to learn about C++11's std::condition_variable . I've read the articles at cppreference.com and cplusplus.com as well as C++0x has no semaphores? How to synchronize threads?. My question, which I think has not been answered by the three noted articles, is: can "semaphores" that are created with a combination of of std::mutex and std::condition_variable (see the answers to C++0x has no semaphores? How to synchronize threads?) be used to synchronize between processes the way posix