2

我是来问问你的意见的。我是一个大项目的新手,所以我将尝试描述我所看到的简单示例。

顶部回溯是

#0  0xb6adfc6d in pthread_mutex_lock () from /usr/lib/libpthread.so.0
#1  0x080d8565 in boost::mutex::lock() ()
#2  0x080d8613 in boost::unique_lock<boost::mutex>::lock() ()
#3  0x080d8642 in boost::unique_lock<boost::mutex>::unique_lock(boost::mutex&)
#4  0x...      in ???    //just ??? in stack
#5  0x...      in ???
#6  0x...      in ???

似乎互斥锁不存在,但它是在类构造函数中创建的。例子:

class A
{
  boost::mutex::scoped_lock mutex_;
public:
  A(): mutex_() {}

  void Read (...)
  {
    //some checks
    boost::mutex::scoped_lock lock(mutex_); // <-- Segfault
    //read
  }

  void Write (...)
  {
    //some checks
    boost::mutex::scoped_lock lock(mutex_);
    //write
  }
};

这对我来说似乎很奇怪,因为我不知道段错误的原因或找到根本原因的可能方法。我很高兴听到您对此的任何建议。

4

1 回答 1

1

It' looks like you're scope locking a scope lock - it's probably a typo

Random example usage: http://www.boost.org/doc/libs/1_53_0/libs/thread/example/mutex.cpp

The usual pattern is to scope the mutex, using the scoped_lock class

boost::recursive_mutex mutex;
void somefunc() {
    boost::unique_lock<boost::recursive_mutex> scoped_lock(mutex);
}
于 2013-05-07T19:05:12.357 回答