• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <thread>

    std::jthread::get_stop_source

    [[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 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

    処理系

    参照