19

I use std::mutex and std::lock_guard in a proper RAII manner throughout my application:

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;
};

It always worked, but I've added parallelism to one more class just now, and in this new class locker throws std::system_error. The problem is here (xthread header):

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

_Mtx_lock returns 3 while the expected value is 0. No idea what 3 means.

VS2013, v120_x64 runtime.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

1 Answers1

21

The error as mentioned by @Phantom (_Thrd_busy) implies that the lock had been recursively taken. Also see this answer

Community
  • 1
  • 1
Werner Erasmus
  • 3,988
  • 17
  • 31
  • 4
    That's it, switching to `std::recursive_mutex` solved the problem! I should probably refactor my code to avoid recursive locking. – Violet Giraffe Jun 09 '15 at 20:55