Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::barrier

      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)
      Condition variables
      (C++11)
      Semaphores
      Latches and Barriers
      (C++20)
      barrier
      (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<barrier>
      template<class CompletionFunction=/* see below */>
      class barrier;
      (since C++20)

      The class templatestd::barrier provides a thread-coordination mechanism that blocks a group of threads of known size until all threads in that group have reached the barrier. Unlikestd::latch, barriers are reusable: once a group of arriving threads are unblocked, the barrier can be reused. Unlikestd::latch, barriers execute a possibly empty callable before unblocking threads.

      A barrier object's lifetime consists of one or more phases. Each phase defines aphase synchronization point where waiting threads block. Threads can arrive at the barrier, but defer waiting on thephase synchronization point by callingarrive. Such threads can later block on thephase synchronization point by callingwait.

      A barrierphase consists of the following steps:

      1. Theexpected count is decremented by each call toarrive orarrive_and_drop.
      2. When the expected count reaches zero, thephase completion step is run, meaning that thecompletion is invoked, and all threads blocked on the phase synchronization point are unblocked. The end of the completion stepstrongly happens-before all calls that were unblocked by the completion step return.
        Exactly once after the expected count reaches zero, a thread executes the completion step during its call toarrive,arrive_and_drop, orwait, except that it is implementation-defined whether the step executes if no thread callswait.
      3. When the completion step finishes, the expected count is reset to the value specified at construction less the number of calls toarrive_and_drop since, and the nextbarrier phase begins.

      Concurrent invocations of the member functions ofbarrier, except for the destructor, do not introduce data races.

      Contents

      [edit]Template parameters

      CompletionFunction - a function object type
      -
      CompletionFunction must meet the requirements ofMoveConstructible andDestructible.std::is_nothrow_invocable_v<CompletionFunction&> must betrue.

      The default template argument ofCompletionFunction is an unspecified function object type that additionally meets the requirements ofDefaultConstructible. Calling an lvalue of it with no arguments has no effects.

      [edit]Member types

      Name Definition
      arrival_token an unspecified object type meeting requirements ofMoveConstructible,MoveAssignable andDestructible

      [edit]Data members

      Member Definition
      CompletionFunctioncompletion a completion function object which is called on every phase completion step
      (exposition-only member object*)

      [edit]Member functions

      constructs abarrier
      (public member function)[edit]
      destroys thebarrier
      (public member function)[edit]
      operator=
      [deleted]
      barrier is not assignable
      (public member function)
      arrives at barrier and decrements the expected count
      (public member function)[edit]
      blocks at the phase synchronization point until its phase completion step is run
      (public member function)[edit]
      arrives at barrier and decrements the expected count by one, then blocks until current phase completes
      (public member function)[edit]
      decrements both the initial expected count for subsequent phases and the expected count for current phase by one
      (public member function)[edit]
      Constants
      [static]
      the maximum value of expected count supported by the implementation
      (public static member function)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_barrier201907L(C++20)std::barrier
      202302L(C++20)
      (DR)
      Relaxed guarantees for phase completion

      [edit]Example

      Run this code
      #include <barrier>#include <iostream>#include <string>#include <syncstream>#include <thread>#include <vector> int main(){constauto workers={"Anil","Busara","Carl"}; auto on_completion=[]()noexcept{// locking not needed herestaticauto phase="... done\n""Cleaning up...\n";std::cout<< phase;        phase="... done\n";};     std::barrier sync_point(std::ssize(workers), on_completion); auto work=[&](std::string name){std::string product="  "+ name+" worked\n";std::osyncstream(std::cout)<< product;// ok, op<< call is atomic        sync_point.arrive_and_wait();         product="  "+ name+" cleaned\n";std::osyncstream(std::cout)<< product;        sync_point.arrive_and_wait();}; std::cout<<"Starting...\n";std::vector<std::jthread> threads;    threads.reserve(std::size(workers));for(autoconst& worker: workers)        threads.emplace_back(work, worker);}

      Possible output:

      Starting...  Anil worked  Carl worked  Busara worked... doneCleaning up...  Busara cleaned  Carl cleaned  Anil cleaned... done

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      P2588R3C++20old phase completion guarantees might prevent hardware accelerationrelaxed

      [edit]See also

      (C++20)
      single-use thread barrier
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/thread/barrier&oldid=176542"

      [8]ページ先頭

      ©2009-2025 Movatter.jp