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

履歴 編集

function
<thread>

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

bool request_stop() noexcept;

概要

スレッドに対する停止要求を作成する。

この関数を実行することで、対象のスレッドがもつstd::stop_token型オブジェクトのstop_requested()メンバ関数がtrueを返すようになる。

効果

メンバ変数として保持しているstd::stop_source型オブジェクトssourceがあるとして、以下と等価:

return ssource.request_stop();

例外

送出しない。

#include <iostream>
#include <thread>

int main()
{
  std::jthread t {
    [](std::stop_token stoken) {
      while (!stoken.stop_requested()) {
        // 停止要求がくるまで処理を継続する…
      }
      std::cout << "exit t1 thread" << std::endl;
    }
  };

  // 停止要求を作成する
  t.request_stop();
  t.join();
}

出力

exit t1 thread

バージョン

言語

  • C++20

処理系