condition-variable

Compilation error : 'this' cannot be implicitly captured in this context

本小妞迷上赌 提交于 2020-08-01 09:32:19
问题 I am trying to add a condition_variable to handle threads, but get a compilation error at this line: this->cv.wait(lk, []{return this->ready;}); Looks like the for the variable this->ready, the 'this' is not in the right scope. In Java this can be handled with TestThread.this, is there anything in C++ doing the same? void TestThread::Thread_Activity() { std::cout << "Thread started \n"; // Wait until ThreadA() sends data { std::unique_lock<std::mutex> lk(m); this->cv.wait(lk, []{return ready;

Mutex protecting std::condition_variable

对着背影说爱祢 提交于 2020-06-23 00:43:42
问题 Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread. Any thread that intends to wait on std::condition_variable has to acquire a std::unique_lock, on the same mutex as used to protect the shared variable http://en.cppreference.com/w/cpp/thread/condition_variable I understand that by protecting the std::condition_variable with a mutex we are protected against missing a notify if the waiting thread is

C++17 atomics and condition_variable deadlock

浪子不回头ぞ 提交于 2020-05-26 12:24:22
问题 I have the following code, which deadlocks on the commented lines. Basically f1 and f2 run as individual threads in the program. f1 expects i to be 1 and decrements it, notifying the cv. f2 expects i to be 0 and increments it, notifying the cv. I assume the deadlock occurs if f2 increments i to 1, calls cv.notify(), then f1 reads a stale value of i (which is 0) because there is no memory synchronization between the mutex and i and then waits and never gets woken up. Then f2 also enters a

Why do I need to acquire a lock to modify a shared “atomic” variable before notifying condition_variable

牧云@^-^@ 提交于 2020-05-21 01:38:45
问题 Accoding to cppreference.com: The thread that intends to modify the variable has to acquire a std::mutex (typically via std::lock_guard) perform the modification while the lock is held execute notify_one or notify_all on the std::condition_variable (the lock does not need to be held for notification) Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread. I'm not quite understand, why modifying a atomic

std::condition_variable checks the condition for the first time, or do you have to wait for someone to make a notify?

家住魔仙堡 提交于 2020-04-30 06:28:21
问题 std::condition_variable checks the condition for the first time, or do you have to wait for someone to make a notify? 回答1: This is answered by reading documentation. It's like: while (!pred()) { wait(lock); } So, the condition is checked first . This can also be shown by testing it (though, to be fair, it's hard to know just from that whether the results are deterministic). 来源: https://stackoverflow.com/questions/61107175/stdcondition-variable-checks-the-condition-for-the-first-time-or-do-you

C++ - Multi-threading - Communication between threads

落花浮王杯 提交于 2020-03-17 12:10:13
问题 #include <iostream> #include <thread> #include <condition_variable> #include <queue> #include <cstdlib> #include <chrono> #include <ctime> #include <random> using namespace std; //counts every number that is added to the queue static long long producer_count = 0; //counts every number that is taken out of the queue static long long consumer_count = 0; void generateNumbers(queue<int> & numbers, condition_variable & cv, mutex & m, bool & workdone){ while(!workdone) { unique_lock<std::mutex> lk

What's the difference between notify_all() and notify_one() of std::condition_variable?

[亡魂溺海] 提交于 2020-02-17 22:42:31
问题 Currently, I am implementing a multi-thread project using std::thread in C++11. I use std::condition_variable to synchronize threads. In detail, one consumer function calls wait() member function of std::condition_variable to wait for task from a global task queue, another producer function generates and puts tasks into the queue. But I do not know the difference between notify_all() and notify_one() member functions of std::condition_variable . Which function should I use in the producer

C++ Producer Consumer stuck in deadlock

余生颓废 提交于 2020-02-06 15:45:29
问题 I'm trying to create a producer-consumer program, where the consumers must keep running until all the producers are finished, then consume what's left in the queue (if there's anything left) and then end. You can check my code bellow, I think I know where the problem (probably deadlock) is, but I don't know how to make it work properly. #include<iostream> #include<cstdlib> #include <queue> #include <thread> #include <mutex> #include <condition_variable> using namespace std; class Company{

Why do pthreads’ condition variable functions require a mutex?

ε祈祈猫儿з 提交于 2020-01-07 01:50:10
问题 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