Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unique_lock<Mutex>::try_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
       
       
      bool try_lock();
      (since C++11)

      Tries to lock (i.e., takes ownership of) the associated mutex without blocking. Effectively callsmutex()->try_lock().

      std::system_error is thrown if there is no associated mutex or if the mutex is already locked by thisstd::unique_lock.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if the ownership of the mutex has been acquired successfully,false otherwise.

      [edit]Exceptions

      • Any exceptions thrown bymutex()->try_lock() (Mutex types do not throw intry_lock, but a customLockable might).

      [edit]Example

      The following examples try to acquire a mutex that was locked and unlocked.

      Run this code
      #include <chrono>#include <iostream>#include <mutex>#include <thread>#include <vector> usingnamespace std::chrono_literals; int main(){std::mutex counter_mutex;std::vector<std::thread> threads;using Id=int; auto worker_task=[&](Id id,std::chrono::seconds wait,std::chrono::seconds acquire){// wait for a few seconds before acquiring lock.std::this_thread::sleep_for(wait); std::unique_lock<std::mutex> lock(counter_mutex,std::defer_lock);if(lock.try_lock())std::cout<<'#'<< id<<", lock acquired.\n";else{std::cout<<'#'<< id<<", failed acquiring lock.\n";return;} // keep the lock for a while.std::this_thread::sleep_for(acquire); std::cout<<'#'<< id<<", releasing lock (via destructor).\n";};     threads.emplace_back(worker_task, Id{0}, 0s, 2s);    threads.emplace_back(worker_task, Id{1}, 1s, 0s);    threads.emplace_back(worker_task, Id{2}, 3s, 0s); for(auto& thread: threads)        thread.join();}

      Output:

      #0, lock acquired.#1, failed acquiring lock.#0, releasing lock (via destructor).#2, lock acquired.#2, releasing lock (via destructor).

      [edit]See also

      locks (i.e., takes ownership of) the associated mutex
      (public member function)[edit]
      attempts to lock (i.e., takes ownership of) the associatedTimedLockable mutex, returns if the mutex has been unavailable for the specified time duration
      (public member function)[edit]
      tries to lock (i.e., takes ownership of) the associatedTimedLockable mutex, returns if the mutex has been unavailable until specified time point has been reached
      (public member function)[edit]
      unlocks (i.e., releases ownership of) the associated mutex
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/unique_lock/try_lock&oldid=161295"

      [8]ページ先頭

      ©2009-2026 Movatter.jp