Home > OS >  std::unique_lock::_Owns data member is not atomic?
std::unique_lock::_Owns data member is not atomic?

Time:10-07

Looking through the std::unique_lock implementation in MSVC14, I noticed it has a data member

bool _Owns;

Since _Owns is used by operator=, operator() and owns_lock() amongst other, I was expecting _Owns to be atomic. Anyone can comment as to why it is not?

Thanks.

CodePudding user response:

A std::unique_lock object cannot be accessed by multiple threads (that would be completely counter to its purpose), so it doesn't need to consider atomicity of the data stored in itself.

The lock object has a reference to a mutex object (e.g. std::mutex) on which it calls member functions to lock or unlock the mutex for the thread owning the lock object. That mutex object is what is shared between threads.

  •  Tags:  
  • c
  • Related