[[nodiscard]]
stop_source get_stop_source() noexcept; // (1) C++20
stop_source get_stop_source() noexcept; // (1) C++26
概要
停止要求を作成するためのstop_sourceオブジェクトを取得する。
戻り値
メンバ変数として保持しているstop_sourceオブジェクトを返す。
例外
送出しない。
例
#include <iostream>
#include <thread>
int main()
{
std::jthread jt1 {
[](std::stop_token stoken) {
while (!stoken.stop_requested()) {}
std::cout << "exit jt1 thread" << std::endl;
}
};
// 別スレッドを立てて、そのスレッドからjt1スレッドに停止要求を発行する
std::jthread jt2 {
[ssource = jt1.get_stop_source()]() {
ssource.request_stop();
}
};
}
出力
exit jt1 thread
バージョン
言語
- C++20
処理系
- Clang:
- GCC: 10.2.0 ✅
- Visual C++: ??
参照
- P2422R1 Remove
nodiscardannotations from the standard library specification- C++26で
[[nodiscard]]指定が削除された
- C++26で