std::shared_lock::shared_lock - cppreference.com (original) (raw)
| shared_lock() noexcept; | (1) | (since C++14) |
|---|---|---|
| shared_lock( shared_lock&& other ) noexcept; | (2) | (since C++14) |
| explicit shared_lock( mutex_type& m ); | (3) | (since C++14) |
| shared_lock( mutex_type& m, std::defer_lock_t t ) noexcept; | (4) | (since C++14) |
| shared_lock( mutex_type& m, std::try_to_lock_t t ); | (5) | (since C++14) |
| shared_lock( mutex_type& m, std::adopt_lock_t t ); | (6) | (since C++14) |
| template< class Rep, class Period > shared_lock( mutex_type& m, const std::chrono::duration<Rep,Period>& timeout_duration ); | (7) | (since C++14) |
| template< class Clock, class Duration > shared_lock( mutex_type& m, const std::chrono::time_point<Clock,Duration>& timeout_time ); | (8) | (since C++14) |
Constructs a shared_lock, optionally locking the supplied mutex.
Constructs a
shared_lockwith no associated mutex.Move constructor. Initializes the
shared_lockwith the contents of other. Leaves other with no associated mutex.
3-8) Constructs a shared_lock with m as the associated mutex. Additionally:
Locks the associated mutex in shared mode by calling m.lock_shared().
Does not lock the associated mutex.
Tries to lock the associated mutex in shared mode without blocking by calling m.try_lock_shared().
Assumes the calling thread already holds a shared lock (i.e., a lock acquired by
lock_shared,try_lock_shared,try_lock_shared_for, ortry_lock_shared_until) on m. The behavior is undefined if not so.Tries to lock the associated mutex in shared mode by calling m.try_lock_shared_for(timeout_duration), which blocks until specified timeout_duration has elapsed or the lock is acquired, whichever comes first. May block for longer than timeout_duration. The behavior is undefined if
Mutexdoes not meet the SharedTimedLockable requirements.Tries to lock the associated mutex in shared mode by calling m.try_lock_shared_until(timeout_time), which blocks until specified timeout_time has been reached or the lock is acquired, whichever comes first. May block for longer than until timeout_time has been reached. The behavior is undefined if
Mutexdoes not meet the SharedTimedLockable requirements.
[edit] Parameters
| other | - | another shared_lock to initialize the state with |
|---|---|---|
| m | - | mutex to associate with the lock and optionally acquire ownership of |
| t | - | tag parameter used to select constructors with different locking strategies |
| timeout_duration | - | maximum duration to block for |
| timeout_time | - | maximum time point to block until |
[edit] Example
Possible output:
#2 read i as 10... #1 read i as 10... #2 woke up... #1 woke up...