From this post, I learned the following:
- I can inherit from
std::mutex
, and check ifm_holder
equals to the current thread id. I think the way is kind of hack, since it is considered a bad idea to inherit from std class. Moreover,m_holder
is some implementation of some C++ library which is not a standard interface - I can use
owns_lock
ofunique_lock
. However, it is a bad idea, since the method only checks if the current lock holds the mutex. We don't useunique_lock
in this way. - A
recursive_mutex
can save us from deadlock when we double lock a mutex in a thread. However, we must firstly check our code design before we decide that we really need to use this. However, I think I have a good design. What I need is a runtime check that if a certain mutex is locked twice by a thread, it will crash rather than dead lock. So arecursive_mutex
seems not suitable for me.