Namespaces
Variants

std::shared_lock<Mutex>:: shared_lock

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
shared_lock ( ) noexcept ;
(1) (C++14부터)
shared_lock ( shared_lock && other ) noexcept ;
(2) (C++14부터)
explicit shared_lock ( mutex_type & m ) ;
(3) (C++14부터)
shared_lock ( mutex_type & m, std:: defer_lock_t t ) noexcept ;
(4) (C++14부터)
shared_lock ( mutex_type & m, std:: try_to_lock_t t ) ;
(5) (C++14부터)
shared_lock ( mutex_type & m, std:: adopt_lock_t t ) ;
(6) (C++14부터)
template < class Rep, class Period >

shared_lock ( mutex_type & m,

const std:: chrono :: duration < Rep,Period > & timeout_duration ) ;
(7) (C++14부터)
template < class Clock, class Duration >

shared_lock ( mutex_type & m,

const std:: chrono :: time_point < Clock,Duration > & timeout_time ) ;
(8) (C++14부터)

shared_lock 을 생성하며, 선택적으로 제공된 뮤텍스를 잠급니다.

1) 연관된 뮤텍스 없이 shared_lock 을 생성합니다.
2) 이동 생성자. shared_lock other 의 내용으로 초기화합니다. other 는 연결된 뮤텍스가 없는 상태로 남습니다.
3-8) 연관된 뮤텍스로 shared_lock 를 생성합니다. 추가적으로:
3) 연결된 뮤텍스를 공유 모드로 잠금니다. 이는 m. lock_shared ( ) 를 호출하여 수행됩니다.
4) 관련된 뮤텍스를 잠그지 않습니다.
5) 연결된 뮤텍스를 공유 모드로 차단 없이 잠그려고 시도합니다. m. try_lock_shared ( ) 를 호출하여 수행합니다.
6) 호출 스레드가 이미 m 에 대한 공유 잠금( lock_shared , try_lock_shared , try_lock_shared_for , 또는 try_lock_shared_until 으로 획득한 잠금)을 보유하고 있다고 가정합니다. 이를 따르지 않을 경우 동작은 정의되지 않습니다.
7) 연결된 뮤텍스를 공유 모드로 잠그려고 시도합니다. m. try_lock_shared_for ( timeout_duration ) 를 호출하여 지정된 timeout_duration 이 경과하거나 잠금이 획득될 때까지 차단됩니다(둘 중 먼저 발생하는 경우). timeout_duration 보다 더 오래 차단될 수 있습니다. Mutex SharedTimedLockable 요구 사항을 충족하지 않으면 동작은 정의되지 않습니다.
8) 연결된 뮤텍스를 공유 모드로 잠그려고 시도합니다. m. try_lock_shared_until ( timeout_time ) 를 호출하여 지정된 timeout_time 에 도달하거나 잠금이 획득될 때까지 차단됩니다(둘 중 먼저 도래하는 시점까지). timeout_time 이 경과한 후에도 더 오래 차단될 수 있습니다. Mutex SharedTimedLockable 요구 사항을 충족하지 않을 경우 동작은 정의되지 않습니다.

매개변수

other - 상태를 초기화하기 위한 다른 shared_lock
m - 뮤텍스와 연결하고 선택적으로 소유권을 획득할 대상
t - 다른 잠금 전략을 가진 생성자를 선택하기 위해 사용되는 태그 매개변수
timeout_duration - 블로킹할 최대 지속 시간
timeout_time - 블로킹할 최대 시간 포인트

예제

#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)
{
     // 두 스레드 모두 정수 i에 접근 가능
     std::shared_lock<std::shared_timed_mutex> slk(m);
     const int ii = i; // 전역 변수 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();
}

가능한 출력:

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