C++ boost::作用域锁定不工作(对我而言)
我正在扩展一个代码库,看看下面从类中提取的代码片段。我尽可能地简单,不让你感到困惑:C++ boost::作用域锁定不工作(对我而言),c++,multithreading,boost,mutex,scoped-lock,C++,Multithreading,Boost,Mutex,Scoped Lock,我正在扩展一个代码库,看看下面从类中提取的代码片段。我尽可能地简单,不让你感到困惑: std::queue< boost::shared_ptr<const Item> > _container; boost::mutex _mutex; //... void foo(Item *item) { boost::mutex::scoped_lock lock(_mutex); std::cout << "enter " << _con
std::queue< boost::shared_ptr<const Item> > _container;
boost::mutex _mutex;
//...
void foo(Item *item)
{
boost::mutex::scoped_lock lock(_mutex);
std::cout << "enter " << _container.size() << " " << this << std::endl;
boost::shared_ptr<const Item> instr(item);
_container.push( instr );
// we only need to signal when size turns from 0 --> 1
if (_container.size() == 1)
{
std::cout << "SIGNALLING" << " " << this << std::endl;
signal();//pop the _container until empty
}
else
{
std::cout <<"NOT SIGNALLING " << _container.size() << " " << this << std::endl;
}
}
……等等。信号不被调用。我打印这个是为了表明我们在同一个物体上操作
在何种情况下发生的可能性有多大foo最初被输入两次,当它被互斥锁保护时,它会弄乱其余的逻辑
我感谢你的解决方案。
多谢各位
在何种情况下发生的可能性有多大foo最初被输入两次,当它被互斥锁保护时,它会弄乱其余的逻辑
当另一个线程访问_容器而不在同一互斥体上同步时,就会发生这种情况。并没有真正回答OP的问题。不,它没有。他的问题是他看到两个进入函数的入口之间没有出口,考虑到互斥,这不应该发生。@SergeyA Oh。是 啊说得好。没有读到足够近的痕迹集中在树枝上。在这种情况下,在提供的代码中不可能发生这种情况。还有别的事情在起作用。
enter 0 0xe919f0
enter 1 0xe919f0
NOT SIGNALLING 2 0xe919f0
enter 2 0xe919f0
NOT SIGNALLING 3 0xe919f0