[thread.lock.guard]

# 32 Concurrency support library [[thread]](./#thread)

## 32.6 Mutual exclusion [[thread.mutex]](thread.mutex#thread.lock.guard)

### 32.6.5 Locks [[thread.lock]](thread.lock#guard)

#### 32.6.5.2 Class template lock_guard [thread.lock.guard]

[ð](#lib:lock_guard)

namespace std {template<class Mutex>class lock_guard {public:using mutex_type = Mutex; explicit lock_guard(mutex_type& m);
 lock_guard(mutex_type& m, adopt_lock_t); ~lock_guard();

 lock_guard(const lock_guard&) = delete;
 lock_guard& operator=(const lock_guard&) = delete; private: mutex_type& pm; // *exposition only*};}

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8203)

An object of type lock_guard controls the ownership of a lockable object
within a scope[.](#1.sentence-1)

A lock_guard object maintains ownership of a lockable
object throughout the lock_guard object's [lifetime](basic.life#def:lifetime "6.8.4 Lifetime [basic.life]")[.](#1.sentence-2)

The behavior of a program is undefined if the lockable object referenced bypm does not exist for the entire lifetime of the lock_guard object[.](#1.sentence-3)

The supplied Mutex type shall meet the [*Cpp17BasicLockable*](thread.req.lockable.basic#:Cpp17BasicLockable "32.2.5.2 Cpp17BasicLockable requirements [thread.req.lockable.basic]") requirements ([[thread.req.lockable.basic]](thread.req.lockable.basic "32.2.5.2 Cpp17BasicLockable requirements"))[.](#1.sentence-4)

[ð](#lib:lock_guard,constructor)

`explicit lock_guard(mutex_type& m);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8218)

*Effects*: Initializes pm with m[.](#2.sentence-1)

Calls m.lock()[.](#2.sentence-2)

[ð](#lib:lock_guard,constructor_)

`lock_guard(mutex_type& m, adopt_lock_t);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8229)

*Preconditions*: The calling thread holds a non-shared lock on m[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8233)

*Effects*: Initializes pm with m[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8237)

*Throws*: Nothing[.](#5.sentence-1)

[ð](#lib:lock_guard,destructor)

`~lock_guard();
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8248)

*Effects*: Equivalent to: pm.unlock()
