如果这是一个错误,我只要不这样做就没有问题,但如果这是预期的行为,我想知道为什么。
我做了这样的事情:
{
boost::lock_guard<boost::mutex> lg(tagsToSocketsMtx);
// mutex protected work
lg.~lock_guard(); // this causes deadlocks later(combined with ...
//...other uses of the same mtx, ofc I use different lock guard in other functions)
// rest of the function
}
最佳答案
一旦 lg
的构造完成,C++ 保证在作用域退出时调用其析构函数无论您是否也在进行显式析构函数调用 .
通过破坏 lg
两次,您将调用未定义的行为,在这种情况下,错误表现为死锁。
关于boost - 显式调用 ~boost::lock_guard<> 会导致互斥锁死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320632/