【问题标题】:std::mutex::lock fails on Windows, error code 3std::mutex::lock 在 Windows 上失败,错误代码 3
【发布时间】:2015-08-24 19:21:29
【问题描述】:

我在整个申请过程中以适当的 RAII 方式使用 std::mutexstd::lock_guard

struct Foo {
    int f() const
    {
       std::lock_guard<std::mutex> locker(m_mutex);
       return m_i;
    }
private:
   int m_i = 0;
   mutable std::mutex m_mutex;
};

它总是有效,但我刚刚在另一个类中添加了并行性,并且在这个新类中 locker 抛出 std::system_error。问题出在这里(xthread 标头):

inline int _Mtx_lockX(_Mtx_t *_Mtx)
{   // throw exception on failure
    return (_Check_C_return(_Mtx_lock(_Mtx)));
}

_Mtx_lock 在预期值为 0 时返回 3。不知道 3 是什么意思。

VS2013,v120_x64 运行时。

【问题讨论】:

  • 贴出实例化互斥锁的代码。
  • 使您的互斥锁可变...您正在从 const 函数修改它的状态。它不应该编译,imo
  • 编译失败here
  • @VioletGiraffe,已接受,但提供无法编译的代码会使我们远离真正的问题。
  • 错误代码 3 似乎是 xthreads.h 顶部 _Thrd_busy 的值。

标签: c++ windows multithreading c++11 mutex


【解决方案1】:

@Phantom (_Thrd_busy) 提到的错误意味着该锁已被递归获取。另见answer

【讨论】:

  • 就是这样,切换到std::recursive_mutex解决了问题!我可能应该重构我的代码以避免递归锁定。
猜你喜欢
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 2014-01-25
  • 2018-06-26
  • 2014-08-21
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
相关资源
最近更新 更多