When I write multi-thread algorithms I find for some methods useful to have expectation about the state of the mutex. For some is it already locked, for some that it is not.
I come up with approach how to assert if the mutex has to be locked already:
ASSERT(!mutex.try_lock_shared()); // assert if the mutex is not uniquely locked
ASSERT(!mutex.try_lock()); // assert if the mutex is not at least shared locked
The question is how I could make assert that checks that the mutex is not locked ... from the current thread. For example if I do this
if (mutex.try_lock()) {
mutex.unlock();
} else {
ASSERT(false); // the mutex is locked
}
it obviously fails from time to time just because some other thread has it locked.