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.
shared_lock
with no associated mutex.shared_lock
with the contents of other
. Leaves other
with no associated mutex.shared_lock
with m
as the associated mutex. Additionally:m.lock_shared()
. The behavior is undefined if this thread already owns the mutex in any mode.m.try_lock_shared()
. The behavior is undefined if this thread already owns the mutex in any mode.m
in shared mode.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.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.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 |
noexcept
specification: noexcept
#include <shared_mutex> #include <iostream> #include <thread> #include <chrono> std::shared_timed_mutex m; int i = 10; void read() { // both the threads get access to the integer i std::shared_lock<std::shared_timed_mutex> slk(m); std::cout << "read i as " << i << "...\n"; // this is not synchronized std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::cout << "woke up...\n"; } int main() { std::thread r1(read); std::thread r2(read); r1.join(); r2.join(); return 0; }
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/thread/shared_lock/shared_lock