Avoid premature wakeup from reader thread by doing the notify AFTER the mutex is released

http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
This commit is contained in:
KjellKod 2014-01-24 13:17:10 -07:00
parent 63616d2acd
commit 0c1845ee18

View File

@ -35,8 +35,10 @@ public:
shared_queue(){}
void push(T item){
std::lock_guard<std::mutex> lock(m_);
queue_.push(std::move(item));
{
std::lock_guard<std::mutex> lock(m_);
queue_.push(std::move(item));
}
data_cond_.notify_one();
}