32 Thread support library [thread]

32.5 Mutual exclusion [thread.mutex]

32.5.4 Mutex requirements [thread.mutex.requirements]

32.5.4.3 Timed mutex types [thread.timedmutex.requirements]

32.5.4.3.1 General [thread.timedmutex.requirements.general]

The timed mutex types are the standard library types timed_­mutex, recursive_­timed_­mutex, and shared_­timed_­mutex.
They meet the requirements set out below.
In this description, m denotes an object of a mutex type, rel_­time denotes an object of an instantiation of duration, and abs_­time denotes an object of an instantiation of time_­point.
[Note 1:
The timed mutex types meet the Cpp17TimedLockable requirements ([thread.req.lockable.timed]).
— end note]
The expression m.try_­lock_­for(rel_­time) is well-formed and has the following semantics:
Preconditions: If m is of type timed_­mutex or shared_­timed_­mutex, the calling thread does not own the mutex.
Effects: The function attempts to obtain ownership of the mutex within the relative timeout ([thread.req.timing]) specified by rel_­time.
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 calling try_­lock()).
The function returns within the timeout specified by rel_­time only if it has obtained ownership of the mutex object.
[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.
— end note]
Synchronization: If try_­lock_­for() returns true, prior unlock() operations on the same object synchronize with ([intro.multithread]) this operation.
Return type: bool.
Returns: true if ownership was obtained, otherwise false.
Throws: Timeout-related exceptions ([thread.req.timing]).
The expression m.try_­lock_­until(abs_­time) is well-formed and has the following semantics:
Preconditions: If m is of type timed_­mutex or shared_­timed_­mutex, the calling thread does not own the mutex.
Effects: The function attempts to obtain ownership of the mutex.
If abs_­time has already passed, the function attempts to obtain ownership without blocking (as if by calling try_­lock()).
The function returns before the absolute timeout ([thread.req.timing]) specified by abs_­time only if it has obtained ownership of the mutex object.
[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.
— end note]
Synchronization: If try_­lock_­until() returns true, prior unlock() operations on the same object synchronize with ([intro.multithread]) this operation.
Return type: bool.
Returns: true if ownership was obtained, otherwise false.
Throws: Timeout-related exceptions ([thread.req.timing]).