std::mutex::lock fails on Windows, error code 3

7.5k views Asked by At

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.

1

There are 1 answers

1
Werner Erasmus On BEST ANSWER

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