Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: further explanation of the lock classes

...

C++ supplies the lock classes lock_guard, unique_lock, and shared_lock, that can be initialized with a mutex. In its constructor, the lock object locks the mutex, and in its destructor, it unlocks the mutex. The lock_guard class provides a simple RAII wrapper around a mutex. The unique_lock and shared_lock classes also use RAII, but provide additional functionality, such as manual control over the locking strategy and allowing ownership of the lock to safely transfer to another object. The unique_lock class prevents the lock from being copied, although it allows the lock ownership to be moved to another lock. The shared_lock class allows the mutex to be shared by several locks. For all three classes, if an exception occurs and takes control flow out of the scope of the lock, the destructor will unlock the mutex and the program can continue working normally. These lock objects are the preferred way to ensure that a mutex is properly released when an exception is thrown.

...