unique_lock
itself is not thread-safe, nor is it meant to be. You are not supposed to access the same unique_lock
instance from multiple threads. It is of course perfectly reasonable to access the same underlying std::mutex
instance from multiple threads, whether directly or via distinct unique_lock
instances that each thread owns.
Therefore, members of unique_lock
do not need to be protected against concurrent access. It is not necessary that _Pmtx->lock();
and _Owns = true;
be performed atomically.
If your concern is that the same thread could create two separate unique_lock
instances that both lock the same mutex - well, yes it could. Such a program would exhibit undefined behavior. It indeed won't be noticed by either unique_lock
instance checking its own _Owns
member. So don't do that.