Namespaces
Variants

std::jthread:: jthread

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
jthread ( ) noexcept ;
(1) (C++20以降)
jthread ( jthread && other ) noexcept ;
(2) (C++20以降)
template < class F, class ... Args >
explicit jthread ( F && f, Args && ... args ) ;
(3) (C++20以降)
jthread ( const jthread & ) = delete ;
(4) (C++20以降)

std::jthread オブジェクトを新しく構築します。

1) スレッドを表さない新しい std::jthread オブジェクトを作成します。
2) ムーブコンストラクタ。 std::jthread オブジェクトを、元々 other が表していた実行スレッドを表すように構築する。この呼び出し後、 other はもはや実行スレッドを表さない。
3) 新しい std::jthread オブジェクトを作成し、実行スレッドと関連付けます。

新しい実行スレッドは以下の実行を開始します:

std:: invoke ( decay-copy ( std:: forward < F > ( f ) ) , get_stop_token ( ) ,
decay-copy ( std:: forward < Args > ( args ) ) ... )
(C++23以前)

std:: invoke ( auto ( std:: forward < F > ( f ) ) , get_stop_token ( ) ,
auto ( std:: forward < Args > ( args ) ) ... )

(C++23以降)

上記の式が適切に形成されている場合、そうでない場合は以下の実行を開始します:

std:: invoke ( decay-copy ( std:: forward < F > ( f ) ) ,
decay-copy ( std:: forward < Args > ( args ) ) ... )
.
(C++23以前)

std:: invoke ( auto ( std:: forward < F > ( f ) ) ,
auto ( std:: forward < Args > ( args ) ) ... )
.

(C++23以降)
decay-copy の呼び出しは評価される (C++23まで) auto によって生成される値は 実体化される (C++23以降) 現在のスレッド内で、これにより引数の評価およびコピー/ムーブ中の例外は新しいスレッドを開始せずに現在のスレッドで送出される。
これらのオーバーロードは、以下の条件でのみオーバーロード解決に参加します: std:: remove_cvref_t < F > std::jthread と同じ型ではない場合。
以下のいずれかが false の場合、プログラムは不適格です:
コンストラクタの呼び出し完了は、 synchronizes with 新しい実行スレッド上の f のコピーの呼び出し開始と同期します。
4) コピーコンストラクタは削除されています。スレッドはコピー可能ではありません。2つの std::jthread オブジェクトが同じ実行スレッドを表すことはできません。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Parameters、Postconditions、Exceptions、Notes、Example、Defect reports、See also)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどはすべて変更せず保持しました - 書式設定と構造は完全に維持しました

パラメータ

other - 別の std::jthread オブジェクト。この std::jthread オブジェクトを構築するために使用
f - Callable オブジェクト。新しいスレッドで実行する対象
args - 新しい関数に渡す引数

事後条件

1) get_id() get_id() std::jthread::id() と等しい(すなわち joinable() false を返す)かつ get_stop_source ( ) . stop_possible ( ) false である。
2) other. get_id ( ) std::jthread::id() と等しく、 get_id() は構築開始前の other. get_id ( ) の値を返す。
3) get_id() get_id() std::jthread::id() と等しくない(つまり joinable() true を返す)場合、かつ get_stop_source ( ) . stop_possible ( ) true である場合。

例外

3) std::system_error スレッドを開始できなかった場合。この例外はエラー状態 std::errc::resource_unavailable_try_again または他の実装固有のエラー状態を表す可能性があります。

注記

スレッド関数への引数は値によって移動またはコピーされます。参照引数をスレッド関数に渡す必要がある場合は、それをラップする必要があります(例: std::ref または std::cref を使用して)。

関数からの戻り値はすべて無視されます。関数が例外を送出した場合、 std::terminate が呼び出されます。戻り値や例外を呼び出し元のスレッドに返すためには、 std::promise または std::async を使用できます。

#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
using namespace std::literals;
void f1(int n)
{
    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(10ms);
    }
}
void f2(int& n)
{
    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(10ms);
    }
}
class foo
{
public:
    void bar()
    {
        for (int i = 0; i < 5; ++i)
        {
            std::cout << "Thread 3 executing\n";
            ++n;
            std::this_thread::sleep_for(10ms);
        }
    }
    int n = 0;
};
class baz
{
public:
    void operator()()
    {
        for (int i = 0; i < 5; ++i)
        {
            std::cout << "Thread 4 executing\n";
            ++n;
            std::this_thread::sleep_for(10ms);
        }
    }
    int n = 0;
};
int main()
{
    int n = 0;
    foo f;
    baz b;
    std::jthread t0; // t0はスレッドではない
    std::jthread t1(f1, n + 1); // 値渡し
    std::jthread t2a(f2, std::ref(n)); // 参照渡し
    std::jthread t2b(std::move(t2a)); // t2bはf2()を実行中。t2aはもはやスレッドではない
    std::jthread t3(&foo::bar, &f); // t3はオブジェクトf上でfoo::bar()を実行
    std::jthread t4(b); // t4はオブジェクトbのコピー上でbaz::operator()を実行
    t1.join();
    t2b.join();
    t3.join();
    std::cout << "Final value of n is " << n << '\n';
    std::cout << "Final value of f.n (foo::n) is " << f.n << '\n';
    std::cout << "Final value of b.n (baz::n) is " << b.n << '\n';
    // t4は破棄時にjoinする
}

出力例:

Thread 2 executing
Thread 1 executing
Thread 4 executing
Thread 3 executing
Thread 3 executing
Thread 4 executing
Thread 2 executing
Thread 1 executing
Thread 3 executing
Thread 1 executing
Thread 4 executing
Thread 2 executing
Thread 3 executing
Thread 1 executing
Thread 4 executing
Thread 2 executing
Thread 3 executing
Thread 1 executing
Thread 4 executing
Thread 2 executing
Final value of n is 5
Final value of f.n (foo::n) is 5
Final value of b.n (baz::n) is 0

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3476 C++20 オーバーロード (3) は直接的に(減衰型の)
F と引数型がmove構築可能であることを要求
これらの
要件を削除 [1]
  1. ムーブ構築可能性は既に std::is_constructible_v によって間接的に要求されている。

関連項目

新しい thread オブジェクトを構築する
( std::thread の公開メンバ関数)
Cドキュメント for thrd_create