Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mutex::try_lock

      From cppreference.com
      <cpp‎ |thread‎ |mutex

      [edit template]
       
       
      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 the mutex. Returns immediately. On successful lock acquisition returnstrue, otherwise returnsfalse.

      This function is allowed to fail spuriously and returnfalse even if the mutex is not currently locked by any other thread.

      Iftry_lock is called by a thread that already owns themutex, the behavior is undefined.

      Priorunlock() operation on the same mutexsynchronizes-with (as defined instd::memory_order) this operation if it returnstrue. Note that priorlock() does not synchronize with this operation if it returnsfalse.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if the lock was acquired successfully, otherwisefalse.

      [edit]Exceptions

      Throws nothing.

      [edit]Example

      [edit]
      Run this code
      #include <chrono>#include <iostream> // std::cout#include <mutex>#include <thread> std::chrono::milliseconds interval(100); std::mutex mutex;int job_shared=0;// both threads can modify 'job_shared',// mutex will protect this variable int job_exclusive=0;// only one thread can modify 'job_exclusive'// no protection needed // this thread can modify both 'job_shared' and 'job_exclusive'void job_1(){std::this_thread::sleep_for(interval);// let 'job_2' take a lock while(true){// try to lock mutex to modify 'job_shared'if(mutex.try_lock()){std::cout<<"job shared ("<< job_shared<<")\n";            mutex.unlock();return;}else{// can't get lock to modify 'job_shared'// but there is some other work to do++job_exclusive;std::cout<<"job exclusive ("<< job_exclusive<<")\n";std::this_thread::sleep_for(interval);}}} // this thread can modify only 'job_shared'void job_2(){    mutex.lock();std::this_thread::sleep_for(5* interval);++job_shared;    mutex.unlock();} int main(){std::thread thread_1(job_1);std::thread thread_2(job_2);     thread_1.join();    thread_2.join();}

      Possible output:

      job exclusive (1)job exclusive (2)job exclusive (3)job exclusive (4)job shared (1)

      [edit]See also

      locks the mutex, blocks if the mutex is not available
      (public member function)[edit]
      unlocks the mutex
      (public member function)[edit]
      C documentation formtx_trylock
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/mutex/try_lock&oldid=131440"

      [8]ページ先頭

      ©2009-2025 Movatter.jp