void notify_all() const noexcept; // (1) C++20
constexpr void notify_all() const noexcept; // (1) C++20
概要
待機している全てのスレッドを起床させる。
この関数は、wait()
関数によるブロッキング待機を解除する。
テンプレートパラメータ制約
- C++26 :
is_const_v<T>
がfalse
であること
効果
起床待機している全てのアトミックオブジェクトの待機を解除する
戻り値
なし
例外
投げない
例
#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();
}
28
std::atomic_ref{x}.notify_all();
#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;
}
}
};
出力
バージョン
言語
- C++20
処理系
- Clang: 9.0 ❌
- GCC: 9.2 ❌
- Visual C++: 2019 Update 3 ❌
参照
- P0514R4 Efficient concurrent waiting for C++20
- ogiroux/atomic_wait - Sample implementation of C++20 atomic_wait/notify
- P1643R1 Add wait/notify to
atomic_ref
- P1960R0 NB Comment Changes Reviewed by SG1
- 宣言に
const
を追加
- 宣言に
- P3323R1 cv-qualified types in
atomic
andatomic_ref
- C++26でCV修飾されたテンプレート引数を受け取れるようになった
- P3309R3
constexpr atomic
andatomic_ref
- C++26で
constexpr
に対応した
- C++26で