std::jthread:: get_stop_token
From cppreference.net
C++
Concurrency support library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::jthread
| Member functions | ||||
| Observers | ||||
| Operations | ||||
| Stop token handling | ||||
|
jthread::get_stop_token
|
||||
| Non-member functions | ||||
|
std::
stop_token
get_stop_token
(
)
const
noexcept
;
|
(C++20以降) | |
内部で保持されている同じ共有停止状態に関連付けられた std::stop_token を返します。
パラメータ
(なし)
戻り値
jthread
オブジェクトが内部に保持する停止状態に関連付けられた、
std::stop_token
型の値。
例
このコードを実行
#include <chrono> #include <condition_variable> #include <iostream> #include <mutex> #include <string_view> #include <thread> using namespace std::chrono_literals; void print(std::string_view name, const std::stop_token& token) { std::cout << name << ": stop_possible = " << token.stop_possible(); std::cout << ", stop_requested = " << token.stop_requested() << '\n'; } void finite_sleepy(std::stop_token stoken) { for (int i = 10; i; --i) { std::this_thread::sleep_for(300ms); if (stoken.stop_requested()) { std::cout << " Sleepy worker is requested to stop\n"; return; } std::cout << " Sleepy worker goes back to sleep\n"; } } void infinite_sleepy() { for (int i = 5; i; --i) { std::this_thread::sleep_for(300ms); std::cout << " Run as long as we want\n"; } } int main() { std::cout << std::boolalpha; // 停止要求を監視するワーカースレッド std::jthread stop_worker(finite_sleepy); // 完了時のみ停止するワーカースレッド std::jthread inf_worker(infinite_sleepy); std::stop_token def_token; std::stop_token stop_token = stop_worker.get_stop_token(); std::stop_token inf_token = inf_worker.get_stop_token(); print("def_token ", def_token); print("stop_token", stop_token); print("inf_token ", inf_token); std::cout << "\nstop_workerの停止要求と結合:\n"; stop_worker.request_stop(); stop_worker.join(); std::cout << "\ninf_workerの停止要求と結合:\n"; inf_worker.request_stop(); inf_worker.join(); std::cout << '\n'; print("def_token ", def_token); print("stop_token", stop_token); print("inf_token ", inf_token); }
出力例:
def_token : stop_possible = false, stop_requested = false stop_token: stop_possible = true, stop_requested = false inf_token : stop_possible = true, stop_requested = false stop_workerの停止要求と結合: Run as long as we want Sleepy worker is requested to stop inf_workerの停止要求と結合: Run as long as we want Run as long as we want Run as long as we want Run as long as we want def_token : stop_possible = false, stop_requested = false stop_token: stop_possible = true, stop_requested = true inf_token : stop_possible = true, stop_requested = true