std::shared_lock<Mutex>::shared_lock

From cppreference.com
< cpp‎ | thread‎ | shared lock
 
 
Concurrency support library
Threads
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
this_thread namespace
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Mutual exclusion
(哋它亢++11)
(哋它亢++11)  
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)    
(哋它亢++14)
Generic lock management
(哋它亢++11)
(哋它亢++17)
(哋它亢++11)
(哋它亢++14)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Condition variables
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Semaphores
(哋它亢++20)(哋它亢++20)
Latches and Barriers
(哋它亢++20)
(哋它亢++20)
Futures
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Safe Reclamation
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)  
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
Hazard Pointers
(哋它亢++26)
(哋它亢++26)

Atomic types
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
Initialization of atomic types
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)
Memory ordering
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Free functions for atomic operations
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++26)(哋它亢++26)
(哋它亢++26)(哋它亢++26)
(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Free functions for atomic flags
(哋它亢++11)(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
 
 
shared_lock() noexcept;
(1) (since 哋它亢++14)
shared_lock( shared_lock&& other ) noexcept;
(2) (since 哋它亢++14)
explicit shared_lock( mutex_type& m );
(3) (since 哋它亢++14)
shared_lock( mutex_type& m, std::defer_lock_t t ) noexcept;
(4) (since 哋它亢++14)
shared_lock( mutex_type& m, std::try_to_lock_t t );
(5) (since 哋它亢++14)
shared_lock( mutex_type& m, std::adopt_lock_t t );
(6) (since 哋它亢++14)
template< class Rep, class Period >

shared_lock( mutex_type& m,

             const std::chrono::duration<Rep,Period>& timeout_duration );
(7) (since 哋它亢++14)
template< class Clock, class Duration >

shared_lock( mutex_type& m,

             const std::chrono::time_point<Clock,Duration>& timeout_time );
(8) (since 哋它亢++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().
4) Does not lock the associated mutex.
5) Tries to lock the associated mutex in shared mode without blocking by calling m.try_lock_shared().
6) Assumes the calling thread already holds a shared lock (i.e., a lock acquired by lock_shared, try_lock_shared, try_lock_shared_for, or try_lock_shared_until) on m. The behavior is undefined if not so.
7) 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 Mutex does not meet the SharedTimedLockable requirements.
8) 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 Mutex does not meet the SharedTimedLockable requirements.

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

Example

#include <chrono>
#include <iostream>
#include <shared_mutex>
#include <syncstream>
#include <thread>
 
std::shared_timed_mutex m;
int i = 10;
 
void read_shared_var(int id)
{
     // both the threads get access to the integer i
     std::shared_lock<std::shared_timed_mutex> slk(m);
     const int ii = i; // reads global i
 
     std::osyncstream(std::cout) << '#' << id << " read i as " << ii << "...\n";
     std::this_thread::sleep_for(std::chrono::milliseconds(10));
     std::osyncstream(std::cout) << '#' << id << " woke up..." << std::endl;
}
 
int main()
{
     std::thread r1{read_shared_var, 1};
     std::thread r2{read_shared_var, 2};
 
     r1.join();
     r2.join();
}

Possible output:

#2 read i as 10...
#1 read i as 10...
#2 woke up...
#1 woke up...