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

履歴 編集

function
<thread>

std::jthread::デストラクタ(C++20)

~jthread();

概要

jthreadオブジェクトを破棄する。

効果

joinabletrueである場合、request_stop()とそのあとjoin()を呼び出す。

#include <iostream>
#include <cstdint>
#include <thread>

std::uint64_t sum1 = 0;
std::uint64_t sum2 = 0;

void f1(std::stop_token stoken, std::uint64_t n)
{
  sum1 = 0;
  for (std::uint64_t i = 1; i < n; ++i) {
    if (stoken.stop_requested()) {
      // 中断リクエストがきたのでスレッドを終了する
      break;
    }
    sum1 += i;
  }
}

void f2(std::uint64_t n)
{
  sum2 = 0;
  for (std::uint64_t i = 1; i < n; ++i) {
    sum2 += i;
  }
}

int main()
{
  {
    // 関数の第1引数がstd::stop_token型である場合、
    // スレッドに中断リクエストを送れるようになる
    std::jthread t1 {f1, 1'000'000};
    std::this_thread::sleep_for(std::chrono::milliseconds{3});

    // スレッド実行する関数がstd::stop_tokenを受け取らない場合、
    // 中断リクエストを使用せず、
    // デストラクタで自動的にjoinするスレッドオブジェクトとして使用する
    std::jthread t2 {
      [] { f2(1'000'000); }
    };
  } // jthreadのデストラクタでは、中断要求を発行し、スレッドの終了を待機する

  std::cout << sum1 << std::endl; // 計算できたところまで表示
  std::cout << sum2 << std::endl;
}

出力例

103102269753
499999500000

バージョン

言語

  • C++20

処理系