[thread.sharedtimedmutex.requirements] # 32 Concurrency support library [[thread]](./#thread) ## 32.6 Mutual exclusion [[thread.mutex]](thread.mutex#thread.sharedtimedmutex.requirements) ### 32.6.4 Mutex requirements [[thread.mutex.requirements]](thread.mutex.requirements#thread.sharedtimedmutex.requirements) #### 32.6.4.5 Shared timed mutex types [thread.sharedtimedmutex.requirements] #### [32.6.4.5.1](#general) General [[thread.sharedtimedmutex.requirements.general]](thread.sharedtimedmutex.requirements.general) [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L7987) The standard library type shared_timed_mutex is a[*shared timed mutex type*](#def:shared_timed_mutex_type "32.6.4.5.1 General [thread.sharedtimedmutex.requirements.general]")[.](#general-1.sentence-1) Shared timed mutex types meet the requirements of timed mutex types ([[thread.timedmutex.requirements]](thread.timedmutex.requirements "32.6.4.3 Timed mutex types")), shared mutex types ([[thread.sharedmutex.requirements]](thread.sharedmutex.requirements "32.6.4.4 Shared mutex types")), and additionally meet the requirements set out below[.](#general-1.sentence-2) In this description,m denotes an object of a shared timed mutex type,rel_time denotes an object of an instantiation ofduration ([[time.duration]](time.duration "30.5 Class template duration")), andabs_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 shared timed mutex types meet the [*Cpp17SharedTimedLockable*](thread.req.lockable.shared.timed#:Cpp17SharedTimedLockable "32.2.5.6 Cpp17SharedTimedLockable requirements [thread.req.lockable.shared.timed]") requirements ([[thread.req.lockable.shared.timed]](thread.req.lockable.shared.timed "32.2.5.6 Cpp17SharedTimedLockable requirements"))[.](#general-1.sentence-4) — *end note*] [2](#general-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8003) The expression m.try_lock_shared_for(rel_time) is well-formed and has the following semantics: [3](#general-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8007) *Preconditions*: The calling thread has no ownership of the mutex[.](#general-3.sentence-1) [4](#general-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8011) *Effects*: Attempts to obtain shared lock ownership for the calling thread 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_shared())[.](#general-4.sentence-2) The function returns within the timeout specified by rel_time only if it has obtained shared 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*] If an exception is thrown then a shared lock has not been acquired for the current thread[.](#general-4.sentence-5) [5](#general-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8029) *Synchronization*: If try_lock_shared_for() returns true, priorunlock() operations on the same object 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#L8035) *Return type*: bool[.](#general-6.sentence-1) [7](#general-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8038) *Returns*: true if the shared lock was acquired, otherwise false[.](#general-7.sentence-1) [8](#general-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8042) *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#L8047) The expression m.try_lock_shared_until(abs_time) is well-formed and has the following semantics: [10](#general-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8051) *Preconditions*: The calling thread has no ownership of the mutex[.](#general-10.sentence-1) [11](#general-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8055) *Effects*: The function attempts to obtain shared ownership of the mutex[.](#general-11.sentence-1) Ifabs_time has already passed, the function attempts to obtain shared ownership without blocking (as if by calling try_lock_shared())[.](#general-11.sentence-2) The function returns before the absolute timeout ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications")) specified by abs_time only if it has obtained shared 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*] If an exception is thrown then a shared lock has not been acquired for the current thread[.](#general-11.sentence-5) [12](#general-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8071) *Synchronization*: If try_lock_shared_until() returns true, priorunlock() operations on the same object 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#L8077) *Return type*: bool[.](#general-13.sentence-1) [14](#general-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8080) *Returns*: true if the shared lock was acquired, otherwise false[.](#general-14.sentence-1) [15](#general-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8084) *Throws*: Timeout-related exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))[.](#general-15.sentence-1) #### [32.6.4.5.2](#thread.sharedtimedmutex.class) Class shared_timed_mutex [[thread.sharedtimedmutex.class]](thread.sharedtimedmutex.class) [🔗](#lib:shared_timed_mutex) namespace std {class shared_timed_mutex {public: shared_timed_mutex(); ~shared_timed_mutex(); shared_timed_mutex(const shared_timed_mutex&) = delete; shared_timed_mutex& operator=(const shared_timed_mutex&) = delete; // exclusive ownershipvoid lock(); // blockingbool try_lock(); templatebool try_lock_for(const chrono::duration& rel_time); templatebool try_lock_until(const chrono::time_point& abs_time); void unlock(); // shared ownershipvoid lock_shared(); // blockingbool try_lock_shared(); templatebool try_lock_shared_for(const chrono::duration& rel_time); templatebool try_lock_shared_until(const chrono::time_point& abs_time); void unlock_shared(); };} [1](#thread.sharedtimedmutex.class-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8123) The class shared_timed_mutex provides a non-recursive mutex with shared ownership semantics[.](#thread.sharedtimedmutex.class-1.sentence-1) [2](#thread.sharedtimedmutex.class-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8127) The class shared_timed_mutex meets all of the shared timed mutex requirements ([thread.sharedtimedmutex.requirements])[.](#thread.sharedtimedmutex.class-2.sentence-1) It is a standard-layout class ([[class.prop]](class.prop "11.2 Properties of classes"))[.](#thread.sharedtimedmutex.class-2.sentence-2) [3](#thread.sharedtimedmutex.class-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L8132) The behavior of a program is undefined if - [(3.1)](#thread.sharedtimedmutex.class-3.1) it destroys a shared_timed_mutex object owned by any thread, - [(3.2)](#thread.sharedtimedmutex.class-3.2) a thread attempts to recursively gain any ownership of a shared_timed_mutex, or - [(3.3)](#thread.sharedtimedmutex.class-3.3) a thread terminates while possessing any ownership of a shared_timed_mutex[.](#thread.sharedtimedmutex.class-3.sentence-1)