##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::shared_lock::shared_lock

From cppreference.com
< cpp‎ | thread‎ | shared lock

shared_lock(); | (1) | (since C++14)
shared_lock( shared_lock&& other ); | (2) | (since C++14)
explicit shared_lock( mutex_type& m ); | (3) | (since C++14)
shared_lock( mutex_type& m, std::defer_lock_t t ); | (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.

1) Constructs a `shared_lock` with no associated mutex.
2) Move constructor. Initializes the `shared_lock` with the contents of `other`. Leaves `other` with no associated mutex.
3-8) Constructs a `shared_lock` with `m` as the associated mutex. Additionally:
3) Locks the associated mutex in shared mode by calling m.lock_shared(). The behavior is undefined if this thread already owns the mutex in any mode.
4) Does not lock the associated mutex.
5) Tries to lock the associated mutexin shared mode without blocking by calling m.try_lock_shared(). The behavior is undefined if this thread already owns the mutex in any mode.
5) Assumes the calling thread already owns `m` in shared mode.
7) Tries to lock the associated mutex in shared mode by calling m.try_lock_shared_until(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 this thread already owns the mutex in any mode.
8) Tries to lock the associated mutex in shared mode by calling m.try_lock_shared_for(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 this thread already owns the mutex in any mode.

### [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] Exceptions

1,2,4)
`noexcept` specification:
noexcept

### [edit] Example

| This section is incomplete Reason: no example

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/thread/shared_lock/shared_lock&oldid=65370"
Category:

- Todo no example