TLDR: Make sure you are not locking a mutex that has been destroyed / hasn't been initialized.
Although the OP has his answer, I thought I would share my issue in case anyone else has the same problem I did.
Notice that the assertion is in __pthread_mutex_lock
and not in the unlock. This, to me, suggests that most other people having this issue are not unlocking a mutex in a different thread than the one that locked it; they are just locking a mutex that has been destroyed.
For me, I had a class (Let's call it Foo
) that registered a static callback function with some other class (Let's call it Bar
). The callback was being passed a reference to Foo
and would occasionally lock/unlock a mutex that was a member of Foo
.
This problem occurred after the Foo
instance was destroyed while the Bar
instance was still using the callback. The callback was being passed a reference to an object that no longer existed and, therefore, was calling __pthread_mutex_lock on garbage memory.
Note, I was using C++11's std::mutex
and std::lock_guard<std::mutex>
, but, since I was on Linux, the problem was exactly the same.
pthread_mutex
es (and mutexes in general) documented such that they must be unlocked by the same thread that locked them? The fact that it happens to work on other platforms is implementation-specific and not portable. – Acne