Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Not only does this save a bit of typing, it guarantees that the lock will be released even if an exception is thrown, because C++ guarantees to call destructors of all local variables on exit from a scope. This also protects you forgetting to release the lock at every exit point from a function with multiple exit points - the compiler takes care of it for you. This technique applies more generally to any situation where you have to acquire and release resources. `stdstd::auto_ptr` ptr is a similar tool for memory management.