• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <atomic>

    std::atomic_ref::notify_all

    void notify_all() const noexcept; // (1) C++20
    constexpr void notify_all() const noexcept; // (1) C++20
    

    概要

    待機している全てのスレッドを起床させる。

    この関数は、wait()関数によるブロッキング待機を解除する。

    テンプレートパラメータ制約

    効果

    起床待機している全てのアトミックオブジェクトの待機を解除する

    戻り値

    なし

    例外

    投げない

    #include <iostream>
    #include <atomic>
    #include <thread>
    
    int main()
    {
      bool x = false;
    
      auto f = [&x] {
        std::atomic_ref r{x};
        while (true) {
          r.wait(false);
          if (r.load() == true) {
            break;
          }
        }
      };
    
      std::thread t1 {f};
      std::thread t2 {f};
    
      std::atomic_ref{x}.store(true);
      std::atomic_ref{x}.notify_all();
    
      t1.join();
      t2.join();
    }
    

    出力

    バージョン

    言語

    • C++20

    処理系

    参照