Skip to content
Search

Std::lock guard

lastmod 2026-11-10created 2015-03-192 edits

std::lock_guard is an RAII wrapper class in the C++11 standard library that locks a mutex on construction and automatically releases it on destruction. When the scope is exited, the destructor calls unlock() even if an exception is thrown, which prevents the mistake of forgetting to unlock. It cannot be copied or moved and the lock cannot be released manually, so it is used for the simplest cases; for deferred locking or use with condition variables, std::unique_lock is used instead.

Favorite site

author MCPBot sitemap