std::shared_mutex
| Defined in header <shared_mutex>
|
||
| class shared_mutex; |
(since C++17) | |
The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a shared_mutex has two levels of access:
- shared - several threads can share ownership of the same mutex.
- exclusive - only one thread can own the mutex.
If one thread has acquired the exclusive lock (through lock, try_lock), no other threads can acquire the lock (including the shared).
If one thread has acquired the shared lock (through lock_shared, try_lock_shared), no other thread can acquire the exclusive lock, but can acquire the shared lock.
Only when the exclusive lock has not been acquired by any thread, the shared lock can be acquired by multiple threads.
Within one thread, only one lock (shared or exclusive) can be acquired at the same time.
Shared mutexes are especially useful when shared data can be safely read by any number of threads simultaneously, but a thread may only write the same data when no other thread is reading or writing at the same time.
The shared_mutex class satisfies all requirements of SharedMutex and StandardLayoutType.
Contents |
[edit] Member types
| Member type | Definition |
native_handle_type (optional*)
|
implementation-defined |
[edit] Member functions
| constructs the mutex (public member function) | |
| destroys the mutex (public member function) | |
| operator= [deleted] |
not copy-assignable (public member function) |
Exclusive locking | |
| locks the mutex, blocks if the mutex is not available (public member function) | |
| tries to lock the mutex, returns if the mutex is not available (public member function) | |
| unlocks the mutex (public member function) | |
| | |
| locks the mutex for shared ownership, blocks if the mutex is not available (public member function) | |
| tries to lock the mutex for shared ownership, returns if the mutex is not available (public member function) | |
| unlocks the mutex (shared ownership) (public member function) | |
Native handle | |
| returns the underlying implementation-defined native handle object (public member function) | |