c++ - boost::mutex,pthread_mutex_destroy 失败 - 调试建议?

标签 c++ boost mutex

我们在静态类中有几个锁(boost::mutex),但是当程序退出时,pthread_mutex_destroy 在互斥量的析构函数中失败(在 boost 中有一个断言检查这个)。

据我所知,pthread_mutex_destroy 只会在两种情况下失败。

[EBUSY]  The implementation has detected an attempt to destroy the object referenced by mutex   while it is locked or referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread.
[EINVAL] The value specified by mutex is invalid.

当我在 GDB 中运行并打印锁时,我看到它已解锁。 不幸的是,我在 GDB 中打印 errno 时遇到了问题。

#3  0x000000000044a2c6 in ~mutex (this=0x847840, __in_chrg=<value optimized out>) at /usr/include/boost/thread/pthread/mutex.hpp:47
47              BOOST_VERIFY(!pthread_mutex_destroy(&m));
(gdb) p m
$1 = {__data = {__lock = 0, __count = 0, __owner = 0, __nusers = 4294967294, __kind = 0, __spins = 0, __list = {__prev = 0x0, 
      __next = 0x0}}, __size = '\000' <repeats 12 times>"\376, \377\377\377", '\000' <repeats 23 times>, __align = 0}

既然我正在写这篇文章,__nusers 和 __size 的值看起来很奇怪。这可能暗示锁无效,但我知道锁在某个时候是有效的(我将 boost::mutex 包装在一个 Lock 类中,我在构造函数、析构函数和锁中打印了 this(0x847840) 的值/解锁功能。

任何关于如何调试它的帮助将不胜感激。

编辑 Locks 类继承自 boost::mutex,并导出一个作用域锁(来自内存):

lock_type::scoped_lock getScopedLock() {
  return lock_type::scoped_lock( *this );
}

我还尝试将锁添加为成员,而不是从它继承,但行为没有变化。 我认为 getScopedLock 函数不会引入任何问题(作用域锁返回 y 值,但由于 RVO 而未创建拷贝),但认为值得一提。 用法如下(我们使用的是c++0x):

auto lock = lock_.getScopedLock();

完整的跟踪:

(gdb) where
#0  0x00007ffff559da75 in *__GI_raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x00007ffff55a15c0 in *__GI_abort () at abort.c:92
#2  0x00007ffff5596941 in *__GI___assert_fail (assertion=0x55851c "!pthread_mutex_destroy(&m)", file=<value optimized out>, line=47, 
    function=0x5595a0 "boost::mutex::~mutex()") at assert.c:81
#3  0x000000000044a2c6 in ~mutex (this=0x847840, __in_chrg=<value optimized out>) at /usr/include/boost/thread/pthread/mutex.hpp:47
#4  0x000000000044d923 in ~Lock (this=0x847840, __in_chrg=<value optimized out>) at include/Locks.h:43
#5  0x00007ffff55a3262 in __run_exit_handlers (status=0) at exit.c:78
#6  *__GI_exit (status=0) at exit.c:100
#7  0x00000000004ea9a6 in start () at src/main.cc:191
#8  0x00000000004de5aa in main (argc=1, argv=0x7fffffffe7b8) at src/main.cc:90

最佳答案

当您解锁您的互斥锁而不先锁定它时,您通常会遇到此错误。

  boost::mutex m;
  m.unlock();

我的猜测是你在某处使用了lockunlock 成员而不是RAII, 并且您丢失了对 lock 的调用。

请注意,大多数时候您不应该调用lockunlock 成员。使用为您调用函数的 scoped_lock

struct s
{
  void foo()
  {
    boost::mutex::scoped_lock l(m_mutex);
    //do something
  }
  private: 
    boost::mutex m_mutex;
};

此外,您提到您继承自 boost::mutex。这可能会导致问题,因为 boost::mutex 没有虚拟析构函数,因此最好不要这样做。

关于c++ - boost::mutex,pthread_mutex_destroy 失败 - 调试建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7165491/

相关文章:

c++ - 检查 C++ 指针是否有效(在 Objective-C(++) 中)

c++ - boost 库是否依赖于 std C++ 库?

c++ - 当你从 boost::fibonacci_heap 中删除一个元素时会发生什么?

c++ - 为什么 sizeof(std::mutex)==40 (gcc,clang,icc)?

c++ - 缩小 std::priority_queue

c++ - 链接器错误(SDL、OpenGL)

c++ - 转换问题无法从 void 转换为 float C++

C++ 错误 : Sleep was not declared in this scope

c# - 如何实现 MVC 4 web App 服务器端互斥体

c++ - 在 C++ 中接受基于线程表现不同的套接字包括