std::thread::thread
提供: cppreference.com
<tbody>
</tbody>
thread() noexcept; |
(1) | (C++11以上) |
thread( thread&& other ) noexcept; |
(2) | (C++11以上) |
template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); |
(3) | (C++11以上) |
thread(const thread&) = delete; |
(4) | (C++11以上) |
新しいスレッドオブジェクトを構築します。
1) スレッドを表さないスレッドオブジェクトを作成します。
2) ムーブコンストラクタ。
other によって表されていた実行のスレッドを表すスレッドオブジェクトを構築します。 呼び出しの後 other は実行のスレッドを表さなくなります。3) 新しい を実行することによって開始されます。 ただし
ただし、引数の評価中、コピー中、ムーブ中に投げられたあらゆる例外が新しいスレッドを開始せずにカレンドスレッド上で投げられるようにするため、
std::thread オブジェクトを作成し、実行のスレッドと紐付けます。 新しい実行のスレッドは std::invoke(decay_copy(std::forward<Function>(f)),
decay_copy(std::forward<Args>(args))...);
decay_copy は以下のように定義されます。
template <class T>
std::decay_t<T> decay_copy(T&& v) { return std::forward<T>(v); }
decay_copy の呼び出しは、呼び出し元の文脈で評価されます。 コンストラクタの呼び出しの完了は、新しい実行のスレッド上の f のコピーの呼び出しの開始に対して同期します (std::memory_order を参照してください)。
|
このコンストラクタは、 std::decay_t<Function> が std::thread と同じ型の場合、オーバーロード解決に参加しません。
|
(C++14以上) |
4) コピーコンストラクタは削除されています。 スレッドはコピー可能ではありません。 2つの
std::thread オブジェクトが同じ実行のスレッドを表すことはありません。引数
| other | - | このスレッドを構築するための別のスレッドオブジェクト |
| f | - | 新しいスレッドで実行する Callable オブジェクト |
| args... | - | 新しい関数に渡す引数 |
事後条件
1) get_id() は std::thread::id() と等しくなります (すなわち joinable が
false になります)2)
other.get_id() は std::thread::id() と等しくなり、 get_id() は構築開始前の other.get_id() の値を返します3) get_id() は std::thread::id() と等しくなくなります (すなわち joinable が
true になります)例外
3) スレッドを開始できなかった場合は std::system_error。 この例外はエラーコンディション
std::errc::resource_unavailable_try_again または処理系固有の別のエラーコンディションを表すかもしれません。ノート
スレッド関数への引数はムーブまたはコピーによって値渡しされます。 スレッド関数に参照引数を渡す必要がある場合は、 (std::ref や std::cref などで) ラップする必要があります。
関数からのあらゆる戻り値は無視されます。 関数が例外を投げた場合は std::terminate が呼ばれます。 呼び出し元スレッドに戻り値や例外を渡すには、 std::promise や std::async を使うことができます。
例
Run this code
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
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 foo::n is " << f.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 foo::n is 5
参考文献
- C++11 standard (ISO/IEC 14882:2011):
- 30.3.1.2 thread constructors [thread.thread.constr]
関連項目
thrd_create の C言語リファレンス
|