std::thread:: thread
|
thread
(
)
noexcept
;
|
(1) | (C++11以降) |
|
thread
(
thread
&&
other
)
noexcept
;
|
(2) | (C++11以降) |
|
template
<
class
F,
class
...
Args
>
explicit thread ( F && f, Args && ... args ) ; |
(3) | (C++11以降) |
|
thread
(
const
thread
&
)
=
delete
;
|
(4) | (C++11以降) |
新しい
std::thread
オブジェクトを構築します。
std::thread
オブジェクトを作成します。
std::thread
オブジェクトを構築する。この呼び出し後、
other
はもはや実行スレッドを表さない。
std::thread
オブジェクトを作成し、実行スレッドに関連付けます。新しい実行スレッドは以下の実行を開始します:
|
INVOKE
(
decay-copy
(
std::
forward
<
F
>
(
f
)
)
,
|
(C++23まで) |
|
std::
invoke
(
auto
(
std::
forward
<
F
>
(
f
)
)
,
|
(C++23以降) |
std::threadと同じ型でない場合。
|
以下のいずれかの条件が満たされる場合、プログラムは不適格です:
|
(C++20まで) |
|
以下のいずれかがfalseの場合、プログラムは不適格です:
|
(C++20以降) |
std::thread
オブジェクトが同じ実行スレッドを表すことはできません。
目次 |
パラメータ
| other | - | このスレッドオブジェクトを構築するための別のスレッドオブジェクト |
| f | - | Callable 新しいスレッドで実行するCallableオブジェクト |
| args | - | 新しい関数に渡す引数 |
事後条件
例外
std::errc::resource_unavailable_try_again
または他の実装固有のエラー状態を表す可能性があります。
注記
スレッド関数への引数は、値によって移動またはコピーされます。参照引数をスレッド関数に渡す必要がある場合は、それをラップする必要があります(例: std::ref または std::cref を使用して)。
関数からの戻り値はすべて無視されます。関数が例外を送出した場合、 std::terminate が呼び出されます。戻り値や例外を呼び出し元のスレッドに戻すためには、 std::promise または std::async を使用できます。
例
#include <chrono> #include <iostream> #include <thread> #include <utility> void f1(int n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 1 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } void f2(int& n) { for (int i = 0; i < 5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } class foo { public: void bar() { for (int i = 0; i < 5; ++i) { std::cout << "Thread 3 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } 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(std::chrono::milliseconds(10)); } } int n = 0; }; int main() { int n = 0; foo f; baz b; std::thread t1; // t1はスレッドではない std::thread t2(f1, n + 1); // 値渡し std::thread t3(f2, std::ref(n)); // 参照渡し std::thread t4(std::move(t3)); // t4がf2()を実行中。t3はもはやスレッドではない std::thread t5(&foo::bar, &f); // t5はオブジェクトf上でfoo::bar()を実行 std::thread t6(b); // t6はオブジェクトbのコピー上でbaz::operator()を実行 t2.join(); t4.join(); t5.join(); t6.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'; }
出力例:
Thread 1 executing Thread 2 executing Thread 3 executing Thread 4 executing Thread 3 executing Thread 1 executing Thread 2 executing Thread 4 executing Thread 2 executing Thread 3 executing Thread 1 executing Thread 4 executing Thread 3 executing Thread 2 executing Thread 1 executing Thread 4 executing Thread 3 executing Thread 1 executing Thread 2 executing Thread 4 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 2097 | C++11 |
オーバーロード
(3)
において、
F
が
std::thread
になり得た
|
F
に制約が追加された
|
| LWG 3476 | C++20 |
オーバーロード
(3)
が直接的に(減衰型の)
F
と引数型がムーブ構築可能であることを要求していた
|
これらの要件を
削除 [1] |
- ↑ move構築可能性は既に間接的に std::is_constructible_v によって要求されている。
参考文献
- C++23標準 (ISO/IEC 14882:2024):
-
- 33.4.3.3 threadコンストラクタ [thread.thread.constr]
- C++20 標準 (ISO/IEC 14882:2020):
-
- 32.4.2.2 スレッドコンストラクタ [thread.thread.constr]
- C++17標準 (ISO/IEC 14882:2017):
-
- 33.3.2.2 threadコンストラクタ [thread.thread.constr]
- C++14規格 (ISO/IEC 14882:2014):
-
- 30.3.1.2 threadコンストラクタ [thread.thread.constr]
- C++11標準 (ISO/IEC 14882:2011):
-
- 30.3.1.2 threadコンストラクタ [thread.thread.constr]
関連項目
new
jthread
オブジェクトを構築
(
std::jthread
のpublicメンバー関数)
|
|
|
Cドキュメント
for
thrd_create
|
|