Documentation Home >> Headers >> <mutex> Header >> std::lock function template

The std::lock function template provides a means of locking more than one mutex at the same time, without risk of deadlock due to inconsistent lock orders.

    typename LockableType1,typename... LockableType2>
void lock(LockableType1& m1,LockableType2& m2...);


The types of the supplied lockable objects LockableType1, LockableType2, ... shall conform to the Lockable requirements.


Acquires a lock on each of the supplied lockable objects m1, m2, ... by an unspecified sequence of calls to the lock(), try_lock() and unlock() members of those types that avoids deadlock.


The current thread owns a lock on each of the supplied lockable objects.


Any exceptions thrown by the calls to lock(), try_lock() and unlock().


If an exception propagates out of the call to std::lock then unlock() shall have been called for any of the objects m1, m2, ... for which a lock has been acquired in the function by a call to lock() or try_lock().


On compilers that don't support variadic templates, an overloaded set of function templates is provided that supports passing of up to 5 arguments.


#include <mutex>

See Also