Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_lock<Mutex>::shared_lock

      From cppreference.com
      <cpp‎ |thread‎ |shared lock
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      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)(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,

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

      shared_lock( mutex_type& m,

                   conststd::chrono::time_point<Clock,Duration>& timeout_time);
      (8)(since C++14)

      Constructs ashared_lock, optionally locking the supplied mutex.

      1) Constructs ashared_lock with no associated mutex.
      2) Move constructor. Initializes theshared_lock with the contents ofother. Leavesother with no associated mutex.
      3-8) Constructs ashared_lock withm as the associated mutex. Additionally:
      3) Locks the associated mutex in shared mode by callingm.lock_shared().
      4) Does not lock the associated mutex.
      5) Tries to lock the associated mutex in shared mode without blocking by callingm.try_lock_shared().
      6) Assumes the calling thread already holds a shared lock (i.e., a lock acquired bylock_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 callingm.try_lock_shared_for(timeout_duration), which blocks until specifiedtimeout_duration has elapsed or the lock is acquired, whichever comes first. May block for longer thantimeout_duration. The behavior is undefined ifMutex does not meet theSharedTimedLockable requirements.
      8) Tries to lock the associated mutex in shared mode by callingm.try_lock_shared_until(timeout_time), which blocks until specifiedtimeout_time has been reached or the lock is acquired, whichever comes first. May block for longer than untiltimeout_time has been reached. The behavior is undefined ifMutex does not meet theSharedTimedLockable requirements.

      [edit]Parameters

      other - anothershared_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

      [edit]Example

      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 istd::shared_lock<std::shared_timed_mutex> slk(m);constint 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...
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/shared_lock/shared_lock&oldid=161226"

      [8]ページ先頭

      ©2009-2025 Movatter.jp