Namespaces
Variants

std::thread:: thread

From ja.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
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 オブジェクトを構築します。

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

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:: decay < F > :: type (C++20まで) std:: remove_cvref_t < F > (C++20以降) がstd::threadと同じ型でない場合。

以下のいずれかの条件が満たされる場合、プログラムは不適格です:

(C++20まで)

以下のいずれかがfalseの場合、プログラムは不適格です:

(C++20以降)
コンストラクタの呼び出し完了は、 synchronizes with 新しい実行スレッド上の f のコピー呼び出し開始と同期する。
4) コピーコンストラクタは削除されています。スレッドはコピーできません。2つの std::thread オブジェクトが同じ実行スレッドを表すことはできません。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters, Postconditions, Exceptions, Notes, Example, Defect reports, References, See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、数値は一切変更していません - 元のフォーマットと構造を完全に保持しています

パラメータ

other - このスレッドオブジェクトを構築するための別のスレッドオブジェクト
f - Callable 新しいスレッドで実行する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()と等しくない 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 を使用できます。

例

#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]
  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