Skip to content
Raymond Chen edited this page Jan 7, 2021 · 2 revisions

Many of the WIL synchronization objects return custom lock guard objects.

These lock guard objects are distinct but follow the same pattern.

The lock guard objects are movable:

  • The moved-to object unlocks any object it had been responsible for.
  • The moved-to object assumes responsibility to unlock the object that was previously the responsibility of the moved-from object.
  • The moved-from object is no longer responsible for unlocking any object.

lock_guard.reset() unlocks the object immediately. The lock guard is no longer responsible for the object .

lock_guard = nullptr; is another way to unlock the object immediately.

lock_guard.release() abandons the object, returning a pointer to the object that needs to be unlocked. The lock guard is no longer responsible for the object .

Gotcha

Despite its name, the release() method does not unlock the object, commonly known as "releasing the lock". The name is inherited from unique_ptr, where release() means "release ownership of the object, and return it, so you can transfer ownership to somebody else." (The lock guard is roughly a unique_ptr with a custom deleter.)