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) (seit C++14)
shared_lock ( shared_lock && other ) noexcept ;
(2) (seit C++14)
explicit shared_lock ( mutex_type & m ) ;
(3) (seit C++14)
shared_lock ( mutex_type & m, std:: defer_lock_t t ) noexcept ;
(4) (seit C++14)
shared_lock ( mutex_type & m, std:: try_to_lock_t t ) ;
(5) (seit C++14)
shared_lock ( mutex_type & m, std:: adopt_lock_t t ) ;
(6) (seit C++14)
template < class Rep, class Period >

shared_lock ( mutex_type & m,

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

shared_lock ( mutex_type & m,

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

Konstruiert einen shared_lock , optional mit Sperren des bereitgestellten Mutex.

1) Konstruiert einen shared_lock ohne zugehöriges Mutex.
2) Move-Konstruktor. Initialisiert den shared_lock mit dem Inhalt von other . Lässt other ohne zugehöriges Mutex zurück.
3-8) Konstruiert einen shared_lock mit m als zugehörigem Mutex. Zusätzlich:
3) Sperrt den zugehörigen Mutex im Shared-Modus durch Aufruf von m. lock_shared ( ) .
4) Sperrt den zugehörigen Mutex nicht.
5) Versucht, den zugehörigen Mutex im Shared-Modus zu sperren, ohne zu blockieren, indem m. try_lock_shared ( ) aufgerufen wird.
6) Nimmt an, dass der aufrufende Thread bereits eine Shared Lock (d.h. eine durch lock_shared , try_lock_shared , try_lock_shared_for , oder try_lock_shared_until erworbene Sperre) auf m hält. Das Verhalten ist undefiniert, wenn dies nicht der Fall ist.
7) Versucht, den assoziierten Mutex im Shared-Modus zu sperren, indem m. try_lock_shared_for ( timeout_duration ) aufgerufen wird. Dies blockiert bis zum Ablauf der angegebenen timeout_duration oder bis die Sperre erworben wird, je nachdem, was zuerst eintritt. Kann länger blockieren als timeout_duration . Das Verhalten ist undefiniert, wenn Mutex die SharedTimedLockable -Anforderungen nicht erfüllt.
8) Versucht, den assoziierten Mutex im Shared-Modus zu sperren, indem m. try_lock_shared_until ( timeout_time ) aufgerufen wird. Dies blockiert bis zum Erreichen des angegebenen timeout_time oder bis die Sperre erworben wird, je nachdem, was zuerst eintritt. Kann länger blockieren als bis timeout_time erreicht wurde. Das Verhalten ist undefiniert, wenn Mutex die SharedTimedLockable Anforderungen nicht erfüllt.

Parameter

other - ein weiteres shared_lock zur Initialisierung des Zustands
m - Mutex, der mit der Sperre assoziiert und optional übernommen werden soll
t - Tag-Parameter zur Auswahl von Konstruktoren mit verschiedenen Sperrstrategien
timeout_duration - maximale Dauer für die Blockierung
timeout_time - maximaler Zeitpunkt für die Blockierung

Beispiel

#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)
{
     // beide Threads erhalten Zugriff auf die Ganzzahl i
     std::shared_lock<std::shared_timed_mutex> slk(m);
     const int ii = i; // liest globale 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();
}

Mögliche Ausgabe:

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