boost :: unique_lock解扰器错误,状态已损坏

时间:2019-01-25 21:22:05

标签: c++ multithreading boost mutex

我有一个IO_Operation函数,它将启动io线程以异步方式执行操作。但是我一次只允许一个IO线程运行,所以我使用唯一的锁来做到这一点。

这是我的代码:


    void MyClass::IO_Operation(...) {
      std::thread(&MyClass::IO_Worker, this, ...).detach();
    }


    void MyClass::IO_Worker(...) {
      boost::unique_lock<boost::shared_mutex> io_thread_lock(this->iothread_mutex_, boost::defer_lock_t());
    bool own_lock = io_thread_lock.try_lock();
    if (!own_lock) return;
    ...
    ...
    ...
    }


代码将产生此错误:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
  what():  boost unique_lock doesn't own the mutex: Operation not permitted

退出函数时发生错误,在boost::unique_lock析构函数中,我在https://www.boost.org/doc/libs/1_58_0/boost/thread/lock_types.hpp查看了boost::unique_lock的源代码

它的析构函数是

~unique_lock()
    {
      if (owns_lock())
      {
        m->unlock();
      }
    }


void unlock()
{
  if (m == 0)
  {
    boost::throw_exception(
        boost::lock_error(static_cast<int>(system::errc::operation_not_permitted), "boost unique_lock has no mutex"));
  }
  if (!owns_lock())
  {
    boost::throw_exception(
        boost::lock_error(static_cast<int>(system::errc::operation_not_permitted), "boost unique_lock doesn't own the mutex"));
  }
  m->unlock();
  is_locked = false;
}

unique_lock的状态似乎已损坏,owns_lock()首先返回true,然后返回false .....此unique_lock是一个锁变量,因此其他线程不可能改变它的状态。

0 个答案:

没有答案