boost - 显式调用 ~boost::lock_guard<> 会导致互斥锁死锁

标签 boost mutex

如果这是一个错误,我只要不这样做就没有问题,但如果这是预期的行为,我想知道为什么。

我做了这样的事情:

{
   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/

相关文章:

c++ - spsc_queue pop - 避免 memcpy?

c++ - 增长 managed_shared_memory 段后出现段错误

c - 在多线程程序中选择互斥锁定和解锁位置的策略

rust - 如何将 String 变量插入全局可变 HashMap(使用 lazy_static 和 Mutex)而不会导致 "does not live long enough"问题?

go - 为什么互斥代码会停止另一个完整的 go-routine?

c++ - 我应该让我的 MutexLock 易变吗?

c++ - 配置 : error: no boost. 找到文件系统库

c++ - 另一个 "ld: symbol(s) not found for architecture x86_64"问题与 boost,这次是 mgiza

c++ - 需要帮助了解有关仅 header 库的 Boost 常见问题解答

c++ - 在私有(private)函数中锁定/解锁互斥锁