mardi 16 août 2016

Multiple std::condition_variable notification to running thread using notify_one()

I'm trying to send multiple notifications to a running thread from another thread (main thread) using std::condition_variable. Sending it once works however doing it the second or multiple times doesn't seem to work. This is what I did (without unnecessary details of the actual events):

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
bool keep_running=true;
bool condition_reached=false;
std::mutex cond_mtx;
std::condition_variable cond;
void thread_waiting_to_be_notified(){
while(keep_running){
    std::unique_lock<std::mutex> lk(cond_mtx);
    cond.wait(lk,[]()->bool{return condition_reached;});
    std::cout << "got notitication" << std::endl;
    condition_reached=false;
}
}
void some_event(){
/*some event happens here*/
}
void another_event(){
/*another event happens here*/
}
int main(){
    std::thread thr(thread_waiting_to_be_notified);
some_event();
std::cout << "some event happened" << std::endl;
condition_reached=true;
std::cout << "another event happened" << std::endl;
cond.notify_one();
another_event();
condition_reached=true;
cond.notify_one();
keep_running=false;
thr.join();
return 0;
}


and the output I got

some event happened
another event happened
got notitication

However, I'd expect

some event happened
another event happened
got notitication
got notitication

Any advice would be appreciated.

Aucun commentaire:

Enregistrer un commentaire