在使用Boost线程的单个生产者/单个消费者应用程序中,如果生产者线程cond_var.notify_one()在消费者线程调用之前进行多次调用,会发生cond_var.wait(lock)什么?
是否会notify_one堆叠额外的呼叫,以便每次呼叫.wait()将与呼叫1:1对应.notify_one()?
编辑实现并发队列的一个常见引用示例有以下方法:
void push(Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
the_condition_variable.notify_one();
}
void wait_and_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
popped_value=the_queue.front();
the_queue.pop();
}
Run Code Online (Sandbox Code Playgroud)
我使用了一些非常相似的代码,并且经历了一些奇怪的内存增长,这似乎可以解释为消费者线程没有为每个人唤醒.notify_one()(因为它仍在忙于其他工作),并且想知道是否缺少"堆叠"可能是原因.
如果(有时)消费者线程无法跟上生产者线程,似乎没有堆叠这个代码就会失败.如果我的理论是正确的,我会很感激有关如何修复此代码的建议.