C++ concepts: SharedTimedMutex
The SharedTimedMutex
concept extends the TimedMutex
concept to include inter-thread synchronization.
[edit] Requirements
Additionally, for object m
of SharedTimedMutex
type supports another mode of ownership: shared. Multiple threads (or, more generally, execution agents) can simulataneously own this mutex in shared mode, but no thread may obtain shared ownership if there is a thread that owns it in exclusive mode and no thread may obtain exclusive ownership if there is a thread that owns it in shared mode. If more than implementation-defined number of threads (no less than 10000) hold a shared lock, another attempt to acquire the mutex in shared mode blocks until the number of shared owners drops down below that threshold.
- The expression m.lock_shared() has the following properties
-
- Behaves as an atomic operation.
- Blocks the calling thread until shared ownership of the mutex can be obtained.
- Prior m.unlock() operations on the same mutex synchronize-with this lock operation (equivalent to release-acquire std::memory_order)
- The behavior is undefined if the calling thread already owns the mutex in any mode.
- If an exception is thrown, the shared lock is not acquired.
- The expression m.try_lock_shared() has the following properties
-
- Behaves as an atomic operation.
- Attempts to obtain shared ownership of the mutex for the calling thread without blocking. If ownership is not obtained, returns immediately. The function is allowed to spuriously fail and return even if the mutex is not currently owned by any threads in any mode.
- If
try_lock_shared()
succeeds, priorunlock()
operations on the same object synchronize-with this operation (equivalent to release-acquire std::memory_order). - The behavior is undefined if the calling thread already owns the mutex in any mode
- The expression m.try_lock_shared_for(duration) has the following properties
-
- Behaves as an atomic operation.
- Attempts to obtain shared ownership of the mutex within the duration specified by
duration
. Ifduration
is less or equalduration.zero()
, attempts to obtain the ownership without locking (as if bytry_lock()
). Otherwise, this function blocks until the mutex is acquired or until the time specified byduration
passes. It returns withinduration
only if it succeeds, but it allowed to fail to acquire the mutex even if at some point in time duringduration
it was not owned by another thread. In any case, it returns true if the mutex was acquired and false otherwise. - If
try_lock_shared_for(duration)
succeeds, priorunlock()
operations on the same object synchronize-with this operation (equivalent to release-acquire std::memory_order). - The behavior is undefined if the calling thread already owns the mutex in any mode
- If an exception is thrown, the shared lock is not acquired.
- The expression m.try_lock_shared_until(time_point) has the following properties
-
- Behaves as an atomic operation.
- Attempts to obtain shared ownership of the mutex within the time left until
time_point
. Iftime_point
already passed, attempts to obtain the ownership without locking (as if bytry_lock()
). Otherwise, this function blocks until the mutex is acquired or until the time specified bytime_point
passes. It returns beforetime_point
only if it succeeds, but it allowed to fail to acquire the mutex even if at some point in time beforetime_point
it was not owned by another thread. In any case, it returns true if the mutex was acquired and false otherwise. - If
try_lock_shared_until(time_point)
succeeds, priorunlock()
operations on the same object synchronize-with this operation (equivalent to release-acquire std::memory_order). - The behavior is undefined if the calling thread already owns the mutex in any mode
- If an exception is thrown, the shared lock is not acquired.
- The expression m.unlock_shared() has the following properties
-
- Behaves as an atomic operation.
- Releases the calling thread's ownership of the mutex and synchronizes-with the subsequent successful lock operations on the same object.
- The behavior is undefined if the calling thread does not own the mutex.
- All lock and unlock operations on a single mutex occur in a single total order
[edit] Library types
The following standard library types satisfy SharedTimedMutex
:
- std::shared_timed_mutex