Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_mutex::lock

      From cppreference.com
      <cpp‎ |thread‎ |shared mutex
       
       
      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
       
       
      void lock();
      (since C++17)

      Acquires an exclusive ownership of theshared_mutex. If another thread is holding an exclusive lock or a shared lock on the sameshared_mutex the a call tolock will block execution until all such locks are released. Whileshared_mutex is locked in an exclusive mode, no other lock of any kind can also be held.

      Iflock is called by a thread that already owns theshared_mutex in any mode (exclusive or shared), the behavior is undefined.A priorunlock() operation on the same mutexsynchronizes-with (as defined instd::memory_order) this operation.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      (none)

      [edit]Exceptions

      Throwsstd::system_error when errors occur, including errors from the underlying operating system that would preventlock from meeting its specifications. The mutex is not locked in the case of any exception being thrown.

      [edit]Notes

      lock() is usually not called directly:std::unique_lock,std::scoped_lock, andstd::lock_guard are used to manage exclusive locking.

      [edit]Example

      Run this code
      #include <chrono>#include <iostream>#include <mutex>#include <shared_mutex>#include <syncstream>#include <thread>#include <vector> std::mutex stream_mutx;void print(autoconst& v){std::unique_lock<std::mutex> lock(stream_mutx);std::cout<<std::this_thread::get_id()<<" saw: ";for(auto e: v)std::cout<< e<<' ';std::cout<<'\n';} int main(){usingnamespace std::chrono_literals;constexprint N_READERS=5;constexprint LAST=-999; std::shared_mutex smtx;int product=0; auto writer=[&smtx,&product](int start,int end){for(int i= start; i< end;++i){auto data= i;{std::unique_lock<std::shared_mutex> lock(smtx);// better than:// smtx.lock();                product= data;}std::this_thread::sleep_for(3ms);}         smtx.lock();// lock manually        product= LAST;        smtx.unlock();}; auto reader=[&smtx,&product]{int data=0;std::vector<int> seen;do{{// better to use:std::shared_lock lock(smtx);// smtx.lock_shared();                data= product;}// smtx.unlock_shared();             seen.push_back(data);std::this_thread::sleep_for(2ms);}while(data!= LAST);         print(seen);}; std::vector<std::thread> threads;    threads.emplace_back(writer,1,13);    threads.emplace_back(writer,42,52); for(int i=0; i< N_READERS;++i)        threads.emplace_back(reader); for(auto&& t: threads)        t.join();}

      Possible output:

      127755840 saw: 43 3 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999144541248 saw: 2 44 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999110970432 saw: 42 2 3 45 4 5 47 6 7 8 8 9 10 11 11 12 -999119363136 saw: 42 2 3 4 46 5 6 7 7 8 9 9 10 11 11 12 12 -999136148544 saw: 2 44 3 4 46 5 6 48 7 8 9 51 10 11 11 12 12 -999

      [edit]See also

      tries to lock the mutex, returns if the mutex is not available
      (public member function)[edit]
      unlocks the mutex
      (public member function)[edit]
      locks the mutex for shared ownership, blocks if the mutex is not available
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/shared_mutex/lock&oldid=156947"

      [8]ページ先頭

      ©2009-2025 Movatter.jp