【发布时间】:2010-11-05 06:15:11
【问题描述】:
这段代码会在生产者void push(data) 中的互斥体上等待吗?
如果是这样,我该如何解决?
boost::mutex access;
boost::condition cond;
// consumer
data read()
{
boost::mutex::scoped_lock lock(access);
// this blocks until the data is ready
cond.wait(lock);
// queue is ready
return data_from_queue();
}
// producer
void push(data)
{
//<--- will a block ever happen here?
boost::mutex::scoped_lock lock(access);
// add data to queue
cond.notify_one();
}
假设我有一个线程池 for(;;) 循环,并且我已经从该池中的线程调用了 read()。然后我处理它的数据。我用一些外部线程调用 push() 。我的问题是,该外部线程能否阻止其对 push(data) 的调用?
【问题讨论】:
标签: c++ boost synchronization conditional-statements