std::barrier

cppreference.com

Example

phase: std::string = "... done\nCleaning up...\n";

main: () = {
    workers: const std::array = ("Anil", "Busara", "Carl");
 
    on_completion:= :() = {
        // locking not needed here
        std::cout << phase;
        phase = "... done\n";
    };
 
    sync_point: std::barrier = (std::ssize(workers), on_completion);
 
    work:= :(name: std::string) = {
        product: std::string = "  (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";
    threads: std::vector<std::jthread> = ();
    threads.reserve(std::size(workers));
    for workers do (worker)
        threads.emplace_back(work, worker);
}

Possible output

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

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.