[thread.lock.algorithm] (original) (raw)

32 Concurrency support library [thread]

32.6 Mutual exclusion [thread.mutex]

32.6.6 Generic locking algorithms [thread.lock.algorithm]

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

Preconditions: Each template parameter type meets the Cpp17Lockable requirements.

[Note 1:

Theunique_lock class template meets these requirements when suitably instantiated.

— _end note_]

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.

If a call totry_lock() fails, unlock() is called for all prior arguments with no further calls to try_lock().

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.

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

Preconditions: Each template parameter type meets the Cpp17Lockable requirements.

[Note 2:

Theunique_lock class template meets these requirements when suitably instantiated.

— _end note_]

Effects: All arguments are locked via a sequence of calls to lock(),try_lock(), or unlock() on each argument.

The sequence of calls does not result in deadlock, but is otherwise unspecified.

[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.

— _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().