std::lock_guard::lock_guard - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::lock_guard::lock_guard

From cppreference.com
< cpp‎ | thread‎ | lock guard

| (1) |
explicit lock_guard( mutex_type& m ); |  | (until C++17)
explicit lock_guard( MutexTypes&... m ); |  | (since C++17)
| (2) |
lock_guard( mutex_type& m, std::adopt_lock_t t ); |  | (until C++17)
lock_guard( MutexTypes&... m, std::adopt_lock_t t ); |  | (since C++17)
lock_guard( const lock_guard& ) = delete; | (3) | (since C++11)

Acquires ownership of the given mutex `m`.

1) Effectively calls m.lock(). The behavior is undefined if `m` is not a recursive mutex and the current thread already owns `m`. 2) Acquires ownership of the mutex m without attempting to lock it. The behavior is undefined if the current thread does not own `m`. | (until C++17)
1) If sizeof...(MutexTypes) == 0, does nothing. Otherwise, If sizeof...(MutexTypes) == 1, effectively calls m.lock(). Otherwise, effectively calls std::lock(m...). The behavior is undefined if one of `MutexTypes` is not a recursive mutex and the current thread already owns the corresponding argument in m.... 2) Acquires ownership of the mutexes m... without attempting to lock any of them. The behavior is undefined if the current thread does not own one (or more) of the mutexes in `m...`. | (since C++17)

3) Copy constructor is deleted.

The behavior is undefined if `m` is destroyed before the `lock_guard` object is.

### [edit] Parameters

m | - | mutex to acquire ownership of
t | - | tag parameter used to select non-locking version of the constructor

### [edit] Exceptions

1) Throws any exceptions thrown by m.lock()

2) Throws nothing

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/thread/lock_guard/lock_guard&oldid=81575"