[thread.timedmutex.requirements]

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

## 32.6 Mutual exclusion [[thread.mutex]](thread.mutex#thread.timedmutex.requirements)

### 32.6.4 Mutex requirements [[thread.mutex.requirements]](thread.mutex.requirements#thread.timedmutex.requirements)

#### 32.6.4.3 Timed mutex types [thread.timedmutex.requirements]

#### [32.6.4.3.1](#general) General [[thread.timedmutex.requirements.general]](thread.timedmutex.requirements.general)

[1](#general-1)

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

The [*timed mutex types*](#def:timed_mutex_types "32.6.4.3.1 General [thread.timedmutex.requirements.general]") are the standard library types timed_mutex,recursive_timed_mutex, and shared_timed_mutex[.](#general-1.sentence-1)

They
meet the requirements set out below[.](#general-1.sentence-2)

In this description, m denotes an object of a mutex type,rel_time denotes an object of an
instantiation of [duration](time.duration "30.5 Class template duration [time.duration]"), and abs_time denotes an
object of an
instantiation of [time_point](time.point "30.6 Class template time_­point [time.point]")[.](#general-1.sentence-3)

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

The timed mutex types meet the [*Cpp17TimedLockable*](thread.req.lockable.timed#:Cpp17TimedLockable "32.2.5.4 Cpp17TimedLockable requirements [thread.req.lockable.timed]") requirements ([[thread.req.lockable.timed]](thread.req.lockable.timed "32.2.5.4 Cpp17TimedLockable requirements"))[.](#general-1.sentence-4)

â *end note*]

[2](#general-2)

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

The expression m.try_lock_for(rel_time) is well-formed
and has the following semantics:

[3](#general-3)

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

*Preconditions*: If m is of type timed_mutex orshared_timed_mutex, the calling thread does not
own the mutex[.](#general-3.sentence-1)

[4](#general-4)

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

*Effects*: The function attempts to obtain ownership of the mutex within the
relative timeout ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))
specified by rel_time[.](#general-4.sentence-1)

If the time specified by rel_time is less than or
equal to rel_time.zero(), the function attempts to obtain ownership without blocking (as if by callingtry_lock())[.](#general-4.sentence-2)

The function returns within the timeout specified byrel_time only if it has obtained ownership of the mutex object[.](#general-4.sentence-3)

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

As
with try_lock(), there is no guarantee that ownership will be obtained if the
lock is available, but implementations are expected to make a strong effort to do so[.](#general-4.sentence-4)

â *end note*]

[5](#general-5)

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

*Synchronization*: If try_lock_for() returns true, prior unlock() operations
on the same object [*synchronize with*](#def:synchronize_with) ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races")) this operation[.](#general-5.sentence-1)

[6](#general-6)

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

*Return type*: bool[.](#general-6.sentence-1)

[7](#general-7)

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

*Returns*: true if ownership was obtained, otherwise false[.](#general-7.sentence-1)

[8](#general-8)

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

*Throws*: Timeout-related exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))[.](#general-8.sentence-1)

[9](#general-9)

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

The expression m.try_lock_until(abs_time) is well-formed
and has the following semantics:

[10](#general-10)

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

*Preconditions*: If m is of type timed_mutex orshared_timed_mutex, the calling thread does not own the
mutex[.](#general-10.sentence-1)

[11](#general-11)

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

*Effects*: The function attempts to obtain ownership of the mutex[.](#general-11.sentence-1)

Ifabs_time has already passed, the function attempts to obtain ownership
without blocking (as if by calling try_lock())[.](#general-11.sentence-2)

The function
returns before the absolute timeout ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications")) specified byabs_time only if it has obtained ownership of the mutex object[.](#general-11.sentence-3)

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

As with try_lock(), there is no guarantee that ownership will
be obtained if the lock is available, but implementations are expected to make a
strong effort to do so[.](#general-11.sentence-4)

â *end note*]

[12](#general-12)

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

*Synchronization*: If try_lock_until() returns true, prior unlock() operations on the same object [*synchronize with*](#def:synchronize_with) ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"))
this operation[.](#general-12.sentence-1)

[13](#general-13)

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

*Return type*: bool[.](#general-13.sentence-1)

[14](#general-14)

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

*Returns*: true if ownership was obtained, otherwise false[.](#general-14.sentence-1)

[15](#general-15)

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

*Throws*: Timeout-related exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))[.](#general-15.sentence-1)

#### [32.6.4.3.2](#thread.timedmutex.class) Class timed_mutex [[thread.timedmutex.class]](thread.timedmutex.class)

[ð](#lib:timed_mutex)

namespace std {class timed_mutex {public: timed_mutex(); ~timed_mutex();

 timed_mutex(const timed_mutex&) = delete;
 timed_mutex& operator=(const timed_mutex&) = delete; void lock(); // blockingbool try_lock(); template<class Rep, class Period>bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); template<class Clock, class Duration>bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); void unlock(); using native_handle_type = *implementation-defined*; // see [[thread.req.native]](thread.req.native "32.2.3 Native handles") native_handle_type native_handle(); // see [[thread.req.native]](thread.req.native "32.2.3 Native handles")};}

[1](#thread.timedmutex.class-1)

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

The class timed_mutex provides a non-recursive mutex with exclusive ownership
semantics[.](#thread.timedmutex.class-1.sentence-1)

If one thread owns a timed_mutex object, attempts by another thread
to acquire ownership of that object will fail (for try_lock()) or block
(for lock(), try_lock_for(), and try_lock_until()) until
the owning thread has released ownership with a call to unlock() or the
call to try_lock_for() or try_lock_until() times out (having
failed to obtain ownership)[.](#thread.timedmutex.class-1.sentence-2)

[2](#thread.timedmutex.class-2)

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

The class timed_mutex meets
all of the timed mutex requirements ([thread.timedmutex.requirements])[.](#thread.timedmutex.class-2.sentence-1)

It is a standard-layout class ([[class.prop]](class.prop "11.2 Properties of classes"))[.](#thread.timedmutex.class-2.sentence-2)

[3](#thread.timedmutex.class-3)

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

The behavior of a program is undefined if

- [(3.1)](#thread.timedmutex.class-3.1)

it destroys a timed_mutex object owned by any thread,

- [(3.2)](#thread.timedmutex.class-3.2)

a thread that owns a timed_mutex object calls lock(),try_lock(), try_lock_for(), or try_lock_until() on that object, or

- [(3.3)](#thread.timedmutex.class-3.3)

a thread terminates while owning a timed_mutex object[.](#thread.timedmutex.class-3.sentence-1)

#### [32.6.4.3.3](#thread.timedmutex.recursive) Class recursive_timed_mutex [[thread.timedmutex.recursive]](thread.timedmutex.recursive)

[ð](#lib:recursive_timed_mutex)

namespace std {class recursive_timed_mutex {public: recursive_timed_mutex(); ~recursive_timed_mutex();

 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; void lock(); // blockingbool try_lock() noexcept; template<class Rep, class Period>bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); template<class Clock, class Duration>bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); void unlock(); using native_handle_type = *implementation-defined*; // see [[thread.req.native]](thread.req.native "32.2.3 Native handles") native_handle_type native_handle(); // see [[thread.req.native]](thread.req.native "32.2.3 Native handles")};}

[1](#thread.timedmutex.recursive-1)

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

The class recursive_timed_mutex provides a recursive mutex with exclusive
ownership semantics[.](#thread.timedmutex.recursive-1.sentence-1)

If one thread owns a recursive_timed_mutex object,
attempts by another thread to acquire ownership of that object will fail (fortry_lock()) or block (for lock(), try_lock_for(), andtry_lock_until()) until the owning thread has completely released
ownership or the call to try_lock_for() or try_lock_until() times out (having failed to obtain ownership)[.](#thread.timedmutex.recursive-1.sentence-2)

[2](#thread.timedmutex.recursive-2)

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

The class recursive_timed_mutex meets
all of the timed mutex requirements ([thread.timedmutex.requirements])[.](#thread.timedmutex.recursive-2.sentence-1)

It is a standard-layout class ([[class.prop]](class.prop "11.2 Properties of classes"))[.](#thread.timedmutex.recursive-2.sentence-2)

[3](#thread.timedmutex.recursive-3)

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

A thread that owns a recursive_timed_mutex object may acquire additional
levels of ownership by calling lock(), try_lock(),try_lock_for(), or try_lock_until() on that object[.](#thread.timedmutex.recursive-3.sentence-1)

It is
unspecified how many levels of ownership may be acquired by a single thread[.](#thread.timedmutex.recursive-3.sentence-2)

If
a thread has already acquired the maximum level of ownership for arecursive_timed_mutex object, additional calls to try_lock(),try_lock_for(), or try_lock_until() fail, and additional
calls to lock() throw an exception of type system_error[.](#thread.timedmutex.recursive-3.sentence-3)

A
thread shall call unlock() once for each level of ownership acquired by
calls to lock(), try_lock(), try_lock_for(), andtry_lock_until()[.](#thread.timedmutex.recursive-3.sentence-4)

Only when all levels of ownership have been released
may ownership of the object be acquired by another thread[.](#thread.timedmutex.recursive-3.sentence-5)

[4](#thread.timedmutex.recursive-4)

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

The behavior of a program is undefined if

- [(4.1)](#thread.timedmutex.recursive-4.1)

it destroys a recursive_timed_mutex object owned by any thread, or

- [(4.2)](#thread.timedmutex.recursive-4.2)

a thread terminates while owning a recursive_timed_mutex object[.](#thread.timedmutex.recursive-4.sentence-1)
