std::shared...:shared_lock(3)
π NAME π
std::shared_lock::shared_lock - std::shared_lock::shared_lock
okay so this is the std::shared...:shared_lock(3) page from the C++ Standard Libary manual β yes, that spelling is literally in the header and we are not fixing it, itβs part of the lore. Weβre talking about the constructor for std::shared_lock, and bestie, weβre gonna build locks. π
π¦ Synopsis π¦
Hereβs the whole overload lineup, copied exactly so nobody gets lost:
shared_lock() noexcept;
(1) (since C++14)
shared_lock( shared_lock&& other ) noexcept;
(2) (since C++14)
explicit shared_lock( mutex_type& m );
(3) (since C++14)
shared_lock( mutex_type& m, std::defer_lock_t t ) noexcept;
(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,
(7) (since C++14)
const std::chrono::duration<Rep,Period>&
timeout_duration );
template< class Clock, class Duration >
shared_lock( mutex_type& m,
(8) (since C++14)
const std::chrono::time_point<Clock,Duration>&
timeout_time );
Constructs a shared_lock, optionally locking the supplied mutex. Thatβs the whole vibe. π
- (1) β Constructs a
shared_lockwith no associated mutex. - (2) β Move constructor. Initializes the
shared_lockwith the contents ofother. Leavesotherwith no associated mutex.
Overloads (3)β(8) construct 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, ortry_lock_shared_until) onm. 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 specifiedtimeout_durationhas elapsed or the lock is acquired, whichever comes first. May block for longer thantimeout_duration. The behavior is undefined ifMutexdoes not meet theSharedTimedLockablerequirements. - (8) β Tries to lock the associated mutex in shared mode by calling
m.try_lock_shared_until(timeout_time), which blocks until specifiedtimeout_timehas been reached or the lock is acquired, whichever comes first. May block for longer than untiltimeout_timehas been reached. The behavior is undefined ifMutexdoes not meet theSharedTimedLockablerequirements.
π¨ bestie, please clock the undefined behavior: overload (6) is UB if the thread isnβt already holding that shared lock, and (7)/8) are UB ifMutexdoesnβt meet theSharedTimedLockablerequirements. That is not a cute surprise. That is undefined behavior. π
π Parameters π
otherβ anothershared_lockto 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 π
Run this code, bestie:
// Run this code
#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...
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::shared...:shared_lock(3) β byeee, go lock something shared, bestie πβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms