• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <atomic>

    std::atomic_notify_all

    namespace std {
      template <class T>
      void
        atomic_notify_all(volatile atomic<T>* object); // (1) C++20
    
      template <class T>
      void
        atomic_notify_all(atomic<T>* object);          // (2) C++20
      template <class T>
      constexpr void
        atomic_notify_all(atomic<T>* object);          // (2) C++26
    }
    

    概要

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

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

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

    • (1) : atomic<T>::is_always_lock_freetrueであること

    効果

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

    戻り値

    なし

    例外

    投げない

    #include <iostream>
    #include <atomic>
    #include <thread>
    
    int main()
    {
      std::atomic<bool> x {false};
    
      auto f = [&x] {
        while (true) {
          std::atomic_wait(&x, false);
          if (std::atomic_load(&x) == true) {
            break;
          }
        }
      };
    
      std::thread t1 {f};
      std::thread t2 {f};
    
      std::atomic_store(&x, true);
      std::atomic_notify_all(&x);
    
      t1.join();
      t2.join();
    }
    

    出力

    バージョン

    言語

    • C++20

    処理系

    関連項目

    参照