[thread.lock.algorithm]

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

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

### 32.6.6 Generic locking algorithms [thread.lock.algorithm]

[ð](#lib:try_lock)

`template<class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);
`

[1](#1)

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

*Preconditions*: Each template parameter type meets the [*Cpp17Lockable*](thread.req.lockable.req#:Cpp17Lockable "32.2.5.3 Cpp17Lockable requirements [thread.req.lockable.req]") requirements[.](#1.sentence-1)

[*Note [1](#note-1)*:

Theunique_lock class template meets these requirements when suitably instantiated[.](#1.sentence-2)

â *end note*]

[2](#2)

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

*Effects*: Calls try_lock() for each argument in order beginning with the
first until all arguments have been processed or a call to try_lock() fails,
either by returning false or by throwing an exception[.](#2.sentence-1)

If a call totry_lock() fails, unlock() is called for all prior arguments
with no further calls to try_lock()[.](#2.sentence-2)

[3](#3)

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

*Returns*: -1 if all calls to try_lock() returned true,
otherwise a zero-based index value that indicates the argument for which try_lock() returned false[.](#3.sentence-1)

[ð](#lib:lock)

`template<class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);
`

[4](#4)

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

*Preconditions*: Each template parameter type meets the [*Cpp17Lockable*](thread.req.lockable.req#:Cpp17Lockable "32.2.5.3 Cpp17Lockable requirements [thread.req.lockable.req]") requirements[.](#4.sentence-1)

[*Note [2](#note-2)*:

Theunique_lock class template meets these requirements when suitably instantiated[.](#4.sentence-2)

â *end note*]

[5](#5)

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

*Effects*: All arguments are locked via a sequence of calls to lock(),try_lock(), or unlock() on each argument[.](#5.sentence-1)

The sequence of calls does
not result in deadlock, but is otherwise unspecified[.](#5.sentence-2)

[*Note [3](#note-3)*:

A deadlock avoidance
algorithm such as try-and-back-off can be used, but the specific algorithm is not
specified to avoid over-constraining implementations[.](#5.sentence-3)

â *end note*]

If a call tolock() or try_lock() throws an exception, unlock() is
called for any argument that had been locked by a call to lock() ortry_lock()[.](#5.sentence-4)
