std::scoped_lock (3) - Linux Manuals
std::scoped_lock: std::scoped_lock
NAME
std::scoped_lock - std::scoped_lock
Synopsis
Defined in header <mutex>
template< class... MutexTypes > (since C++17)
class scoped_lock;
The class scoped_lock is a mutex wrapper that provides a convenient RAII-style mechanism for owning one or more mutexes for the duration of a scoped block.
When a scoped_lock object is created, it attempts to take ownership of the mutexes it is given. When control leaves the scope in which the scoped_lock object was created, the scoped_lock is destructed and the mutexes are released, in reverse order. If several mutexes are given, deadlock avoidance algorithm is used as if by std::lock.
The scoped_lock class is non-copyable.
Template parameters
MutexTypes - the types of the mutexes to lock. The types must meet the Lockable requirements unless sizeof...(MutexTypes)==1, in which case the only type must meet BasicLockable
Member types
Member type Definition
mutex_type (if sizeof...(MutexTypes)==1) Mutex, the sole type in MutexTypes...
Member functions
constructor (public member function)
destructor (public member function)
operator= not copy-assignable
[deleted]
Example
The following example uses std::scoped_lock to lock pairs of mutexes without deadlock and is RAII-style.
// Run this code