Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unique_lock<Mutex>::unique_lock

      From cppreference.com
      <cpp‎ |thread‎ |unique 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
       
       
      unique_lock()noexcept;
      (1)(since C++11)
      unique_lock( unique_lock&& other)noexcept;
      (2)(since C++11)
      explicit unique_lock( mutex_type& m);
      (3)(since C++11)
      unique_lock( mutex_type& m,std::defer_lock_t t)noexcept;
      (4)(since C++11)
      unique_lock( mutex_type& m,std::try_to_lock_t t);
      (5)(since C++11)
      unique_lock( mutex_type& m,std::adopt_lock_t t);
      (6)(since C++11)
      template<class Rep,class Period>

      unique_lock( mutex_type& m,

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

      unique_lock( mutex_type& m,

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

      Constructs aunique_lock, optionally locking the supplied mutex.

      1) Constructs aunique_lock with no associated mutex.
      2) Move constructor. Initializes theunique_lock with the contents ofother. Leavesother with no associated mutex.
      3-8) Constructs aunique_lock withm as the associated mutex. Additionally:
      3) Locks the associated mutex by callingm.lock().
      4) Does not lock the associated mutex.
      5) Tries to lock the associated mutex without blocking by callingm.try_lock(). The behavior is undefined ifMutex does not satisfyLockable.
      6) Assumes the calling thread already holds a non-shared lock (i.e., a lock acquired bylock,try_lock,try_lock_for, ortry_lock_until) onm. The behavior is undefined if not so.
      7) Tries to lock the associated mutex by callingm.try_lock_for(timeout_duration). 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 satisfyTimedLockable.
      8) Tries to lock the associated mutex by callingm.try_lock_until(timeout_time). 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 satisfyTimedLockable.

      [edit]Parameters

      other - anotherunique_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 <iostream>#include <mutex>#include <thread>#include <utility>#include <vector> std::mutex m_a, m_b, m_c;int a, b, c=1; void update(){{// Note: std::lock_guard or atomic<int> can be used insteadstd::unique_lock<std::mutex> lk(m_a);++a;} {// Note: see std::lock and std::scoped_lock for details and alternativesstd::unique_lock<std::mutex> lk_b(m_b,std::defer_lock);std::unique_lock<std::mutex> lk_c(m_c,std::defer_lock);std::lock(lk_b, lk_c);        b=std::exchange(c, b+ c);}} int main(){std::vector<std::thread> threads;for(unsigned i=0; i<12;++i)        threads.emplace_back(update); for(auto& i: threads)        i.join(); std::cout<< a<<"'th and "<< a+1<<"'th Fibonacci numbers: "<< b<<" and "<< c<<'\n';}

      Output:

      12'th and 13'th Fibonacci numbers: 144 and 233
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/unique_lock/unique_lock&oldid=161298"

      [8]ページ先頭

      ©2009-2026 Movatter.jp