condition-variable

Share condition variable & mutex between processes: does mutex have to locked before?

两盒软妹~` 提交于 2019-11-26 22:16:49
I need to some little help to understand how to use condition variables in C to resolve an exercise. Here is a little example: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #define OKTOWRITE "/oktowrite" #define MESSAGE "/message" #define MUTEX "/lock" int main(int argc, char** argv) { pthread_cond_t* condition; pthread_mutex_t *mutex; char* message; int des_cond, des_msg, des_mutex; int mode = S_IRWXU | S_IRWXG; des_mutex = shm_open

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

 ̄綄美尐妖づ 提交于 2019-11-26 21:54:55
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 cv_m; int i = 0; bool done = false; void waits() { std::unique_lock<std::mutex> lk(cv_m); std::cout <<

What happens when calling the destructor of a thread object that has a condition variable waiting?

有些话、适合烂在心里 提交于 2019-11-26 20:59:24
问题 I am using a SynchronisedQueue to communicate between threads. I found that destroying the thread object when the attaching thread is waiting on a condition variable would cause the program crash. This can be corrected by calling detach() before the thread destruction. But I am wondering what happens exactly when a thread waiting a conditional variable got terminated. Is there another way to use condition variable to avoid this? #include <queue> #include <thread> #include <mutex> #include

condition variable - why calling pthread_cond_signal() before calling pthread_cond_wait() is a logical error?

让人想犯罪 __ 提交于 2019-11-26 20:21:49
It's written in POSIX threads tutorial https://computing.llnl.gov/tutorials/pthreads/ that it is a logical error. my question is why it is a logical error? In my program i need to use these signals, however i cannot guarantee that there will be a thread that will be in _cond_wait state. I tried to test it and nothing happens. Is this can cause unexpected behavior or worse? thank you! The answer of blaze comes closest, but is not totally clear: conditional variables should only be used to signal a change in a condition . Thread 1 checks a condition. If the condition doesn't meet, he waits on

C++11 thread-safe queue

半腔热情 提交于 2019-11-26 18:23:57
A project I'm working on uses multiple threads to do work on a collection of files. Each thread can add files to the list of files to be processed, so I put together (what I thought was) a thread-safe queue. Relevant portions follow: // qMutex is a std::mutex intended to guard the queue // populatedNotifier is a std::condition_variable intended to // notify waiting threads of a new item in the queue void FileQueue::enqueue(std::string&& filename) { std::lock_guard<std::mutex> lock(qMutex); q.push(std::move(filename)); // Notify anyone waiting for additional files that more have arrived

Using condition variable in a producer-consumer situation

你说的曾经没有我的故事 提交于 2019-11-26 16:13:41
问题 I'm trying to learn about condition variables and how to use it in a producer-consumer situation. I have a queue where one thread pushes numbers into the queue while another thread popping numbers from the queue. I want to use the condition variable to signal the consuming thread when there is some data placed by the producing thread. The problem is there are times (or most times) that it only pushes up to two items into the queue then hangs. I have indicated in the produce() function where

Share condition variable & mutex between processes: does mutex have to locked before?

北战南征 提交于 2019-11-26 12:19:24
问题 I need to some little help to understand how to use condition variables in C to resolve an exercise. Here is a little example: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #define OKTOWRITE \"/oktowrite\" #define MESSAGE \"/message\" #define MUTEX \"/lock\" int main(int argc, char** argv) { pthread_cond_t* condition; pthread_mutex_t

When is a condition variable needed, isn&#39;t a mutex enough?

和自甴很熟 提交于 2019-11-26 11:57:34
问题 I\'m sure mutex isn\'t enough that\'s the reason the concept of condition variables exist; but it beats me and I\'m not able to convince myself with a concrete scenario when a condition variable is essential. Differences between Conditional variables, Mutexes and Locks question\'s accepted answer says that a condition variable is a lock with a \"signaling\" mechanism. It is used when threads need to wait for a resource to become available. A thread can \"wait\" on a CV and then the resource

Calling pthread_cond_signal without locking mutex

左心房为你撑大大i 提交于 2019-11-26 11:31:26
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? If you do not lock the mutex in the codepath that changes the condition and signals, you can lose wakeups. Consider

condition variable - why calling pthread_cond_signal() before calling pthread_cond_wait() is a logical error?

只谈情不闲聊 提交于 2019-11-26 07:34:44
问题 It\'s written in POSIX threads tutorial https://computing.llnl.gov/tutorials/pthreads/ that it is a logical error. my question is why it is a logical error? In my program i need to use these signals, however i cannot guarantee that there will be a thread that will be in _cond_wait state. I tried to test it and nothing happens. Is this can cause unexpected behavior or worse? thank you! 回答1: The answer of blaze comes closest, but is not totally clear: conditional variables should only be used