std::jthread:: jthread
|
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
オブジェクトを新しく構築します。
std::jthread
オブジェクトを作成します。
std::jthread
オブジェクトを、元々
other
が表していた実行スレッドを表すように構築する。この呼び出し後、
other
はもはや実行スレッドを表さない。
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
(
)
,
|
(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
)
)
,
|
(C++23以降) |
- std:: is_constructible_v < std:: decay_t < F > , F >
- ( std:: is_constructible_v < std:: decay_t < Args > , Args > && ... )
-
std::
is_invocable_v
<
std::
decay_t
<
F
>
,
std::
decay_t
<
Args
>
...
>
||
std:: is_invocable_v < std:: decay_t < F > , std:: stop_token , std:: decay_t < Args > ... >
std::jthread
オブジェクトが同じ実行スレッドを表すことはできません。
目次 |
パラメータ
| other | - |
別の
std::jthread
オブジェクト。この
std::jthread
オブジェクトを構築するために使用
|
| f | - | Callable オブジェクト。新しいスレッドで実行する対象 |
| args | - | 新しい関数に渡す引数 |
事後条件
get_id()
get_id()
は
std::jthread::id()
と等しい(すなわち
joinable()
は
false
を返す)かつ
get_stop_source
(
)
.
stop_possible
(
)
は
false
である。
get_id()
get_id()
が
std::jthread::id()
と等しくない(つまり
joinable()
が
true
を返す)場合、かつ
get_stop_source
(
)
.
stop_possible
(
)
が
true
である場合。
例外
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] |
- ↑ ムーブ構築可能性は既に std::is_constructible_v によって間接的に要求されている。
関連項目
新しい
thread
オブジェクトを構築する
(
std::thread
の公開メンバ関数)
|
|
|
Cドキュメント
for
thrd_create
|
|