##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# C++ concepts: SharedMutex

From cppreference.com
< cpp‎ | concept

The `SharedMutex` concept extends the `TimedMutex` concept to include inter-thread synchronization.

### [edit] Requirements

- `TimedMutex`

Additionally, for object `m` of `SharedMutex` 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.

- 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, prior `unlock()` 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`. If `duration` is less or equal `duration.zero()`, attempts to obtain the ownership without locking (as if by `try_lock()`). Otherwise, this function blocks until the mutex is acquired or until the time specified by `duration` passes. It returns within `duration` only if it succeeds, but it allowed to fail to acquire the mutex even if at some point in time during `duration` 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, prior `unlock()` 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_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`. If `time_point` already passed, attempts to obtain the ownership without locking (as if by `try_lock()`). Otherwise, this function blocks until the mutex is acquired or until the time specified by `time_point` passes. It returns before `time_point` only if it succeeds, but it allowed to fail to acquire the mutex even if at some point in time before `time_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, prior `unlock()` 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.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 `SharedMutex`:

- std::shared_mutex

### [edit] See also

- Thread support library
- `Mutex`
- `TimedMutex`

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/concept/SharedMutex&oldid=65582"