Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::try_lock

      From cppreference.com
      <cpp‎ |thread
       
       
      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)
      try_lock
      (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
       
      Defined in header<mutex>
      template<class Lockable1,class Lockable2,class...LockableN>
      int try_lock( Lockable1& lock1, Lockable2& lock2, LockableN&...lockn);
      (since C++11)

      Tries to lock each of the givenLockable objectslock1,lock2,...,lockn by callingtry_lock in order beginning with the first.

      If a call totry_lock fails, no further call totry_lock is performed,unlock is called for any locked objects and a0-based index of the object that failed to lock is returned.

      If a call totry_lock results in an exception,unlock is called for any locked objects before rethrowing.

      Contents

      [edit]Parameters

      lock1, lock2, ..., lockn - theLockable objects to lock

      [edit]Return value

      -1 on success, or0-based index value of the object that failed to lock.

      [edit]Example

      The following example usesstd::try_lock to periodically tally and reset counters running in separate threads.

      Run this code
      #include <chrono>#include <functional>#include <iostream>#include <mutex>#include <thread>#include <vector> int main(){int foo_count=0;std::mutex foo_count_mutex;int bar_count=0;std::mutex bar_count_mutex;int overall_count=0;bool done=false;std::mutex done_mutex; auto increment=[](int& counter,std::mutex& m,constchar* desc){for(int i=0; i<10;++i){std::unique_lock<std::mutex> lock(m);++counter;std::cout<< desc<<": "<< counter<<'\n';            lock.unlock();std::this_thread::sleep_for(std::chrono::seconds(1));}}; std::thread increment_foo(increment,std::ref(foo_count),std::ref(foo_count_mutex),"foo");std::thread increment_bar(increment,std::ref(bar_count),std::ref(bar_count_mutex),"bar"); std::thread update_overall([&](){        done_mutex.lock();while(!done){            done_mutex.unlock();int result= std::try_lock(foo_count_mutex, bar_count_mutex);if(result==-1){                overall_count+= foo_count+ bar_count;                foo_count=0;                bar_count=0;std::cout<<"overall: "<< overall_count<<'\n';                foo_count_mutex.unlock();                bar_count_mutex.unlock();}std::this_thread::sleep_for(std::chrono::seconds(2));            done_mutex.lock();}        done_mutex.unlock();});     increment_foo.join();    increment_bar.join();    done_mutex.lock();    done=true;    done_mutex.unlock();    update_overall.join(); std::cout<<"Done processing\n"<<"foo: "<< foo_count<<'\n'<<"bar: "<< bar_count<<'\n'<<"overall: "<< overall_count<<'\n';}

      Possible output:

      bar: 1foo: 1foo: 2bar: 2foo: 3overall: 5bar: 1foo: 1bar: 2foo: 2bar: 3overall: 10bar: 1foo: 1bar: 2foo: 2overall: 14bar: 1foo: 1bar: 2overall: 17foo: 1bar: 1foo: 2overall: 20Done processingfoo: 0bar: 0overall: 20

      [edit]See also

      (C++11)
      locks specified mutexes, blocks if any are unavailable
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/try_lock&oldid=161290"

      [8]ページ先頭

      ©2009-2025 Movatter.jp