Documentation Home >> Headers >> <mutex> Header >> std::lock_guard class template

The std::lock_guard class template provides a basic lock ownership wrapper. The type of mutex being locked is specified by template parameter Mutex, and must meet the Lockable requirements. The specified mutex is locked in the constructor and unlocked in the destructor. This provides a simple means of locking a mutex for a block of code and ensuring that the mutex is unlocked when the block is left, whether that is by running off the end, by the use of a control flow statement such as break or return, or by throwing an exception.

Instances of std::lock_guard are neither MoveConstructible, CopyConstructible or CopyAssignable.

template <class Mutex>
class lock_guard
{
public:
    typedef Mutex mutex_type;

    explicit lock_guard(mutex_type& m);
    lock_guard(mutex_type& m, adopt_lock_t);
    ~lock_guard();

    lock_guard(lock_guard const& ) = delete;
    lock_guard& operator=(
        lock_guard const& ) = delete;
};
Header

#include <mutex>

See Also