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

履歴 編集

function
<thread>

std::jthread::get_stop_source(C++20)

[[nodiscard]] stop_source get_stop_source() noexcept;

概要

停止要求を作成するためのstop_sourceオブジェクトを取得する。

戻り値

メンバ変数として保持しているstop_sourceオブジェクトを返す。

例外

送出しない。

#include <iostream>
#include <thread>

int main()
{
  std::jthread t1 {
    [](std::stop_token stoken) {
      while (!stoken.stop_requested()) {}
      std::cout << "exit t1 thread" << std::endl;
    }
  };

  // 別スレッドを立てて、そのスレッドからt1スレッドに停止要求を発行する
  std::jthread t2 {
    [ssource = t1.get_stop_source()]() {
      ssource.request_stop();
    }
  };
}

出力

exit t1 thread

バージョン

言語

  • C++20

処理系