名前空間
変種
操作

std::thread::thread

提供: cppreference.com
< cpp‎ | thread‎ | thread


 
 
スレッドのサポートライブラリ
スレッド
Original:
Threads
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread(C++11)
this_thread空間
Original:
this_thread namespace
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
get_id(C++11)
yield(C++11)
sleep_for(C++11)
sleep_until(C++11)
相互排除
Original:
Mutual exclusion
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
mutex(C++11)
timed_mutex(C++11)
汎用のロック管理
Original:
Generic lock management
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
lock_guard(C++11)
unique_lock(C++11)
defer_lock_t
try_to_lock_t
adopt_lock_t
(C++11)
(C++11)
(C++11)
lock(C++11)
try_lock(C++11)
defer_lock
try_to_lock
adopt_lock
(C++11)
(C++11)
(C++11)
条件変数
Original:
Condition variables
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
condition_variable(C++11)
condition_variable_any(C++11)
notify_all_at_thread_exit(C++11)
cv_status(C++11)
先物
Original:
Futures
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
promise(C++11)
future(C++11)
shared_future(C++11)
packaged_task(C++11)
async(C++11)
launch(C++11)
future_status(C++11)
future_error(C++11)
future_category(C++11)
future_errc(C++11)
 
std::thread
メンバークラス
Original:
Member classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread::id
メンバ関数
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread::thread
thread::~thread
thread::operator=
オブザーバー
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread::joinable
thread::get_id
thread::native_handle
thread::hardware_concurrency
操作
Original:
Operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
thread::join
thread::detach
thread::swap
非メンバ関数
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::swap
 
thread();
(1) (C++11およびそれ以降)
thread( thread&& other );
(2) (C++11およびそれ以降)
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
(3) (C++11およびそれ以降)
thread(const thread&) = delete;
(4) (C++11およびそれ以降)
新しいThreadオブジェクトを構築します.
Original:
Constructs new thread object.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
スレッドを表すものではありません新しいThreadオブジェクトを作成します。.
Original:
Creates new thread object which does not represent a thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
コンストラクタを移動します。 otherで表現されたスレッドの実行を表現するために、スレッドオブジェクトを構築します。このコールの後otherもはや実行中のスレッドを表していません.
Original:
Move constructor. Constructs the thread object to represent the thread of execution that was represented by other. After this call other no longer represents a thread of execution.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
実行のスレッドに新たなstd::threadオブジェクトと関連付け、それを作成します。最初のコンストラクタは関数であるかのようにスレッドローカルストレージへのすべての引数のargs...をコピーします
Original:
Creates new std::thread object and associates it with a thread of execution. First the constructor copies all arguments args... to thread-local storage as if by the function:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
template <class T>
typename decay<T>::type decay_copy(T&& v) {
    return std::forward<T>(v);
}
@ @引数の評価やコピー中にスローされた例外は、現在のスレッドではなく、新しいスレッドでスローされた.
Original:
@@ Any exceptions thrown during evaluation and copying of the arguments are thrown in the current thread, not the new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
@ @新しいスレッドで実行されるコードは次のように定義される。 {{tr|}Nonecopied_argsを参照してみましょう}と<code>t1t2、...、tN、どこNですsizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:
</code>
  • fがクラスTのメンバ関数へのポインタであるなら、それが呼び出されます。戻り値は無視されます。効果的に、次のコードが実行されます
    Original:
    If f is pointer to a member function of class T, then it is called. The return value is ignored. Effectively, the following code is executed:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • (t1.*f)(t2, ..., tN)t1のタイプはどちらTである場合、またはTTから派生した型への参照の.
    Original:
    (t1.*f)(t2, ..., tN) if the type of t1 is either T, reference to T or reference to type derived from T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ((*t1).*f)(t2, ..., tN)その他のとき.
    Original:
    ((*t1).*f)(t2, ..., tN) otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • N == 1場合とf、それはクラスのメンバデータオブジェクトへのポインタにアクセスされています。オブジェクトの値は無視されます。効果的に、次のコードが実行されます
    Original:
    If N == 1 and f is pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • t1.*fいただくとt1のタイプはどちらTですTまたはTから派生した型への参照の.
    Original:
    t1.*f if and the type of t1 is either T, reference to T or reference to type derived from T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • (*t1).*fその他のとき.
    Original:
    (*t1).*f otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • f他のすべてのケースで非メンバ関数へのポインタと呼ばれている。戻り値は無視されます。効果的に、f(t1, t2, ..., tN)が実行されます.
    Original:
    f is called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively, f(t1, t2, ..., tN) is executed.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Original:
@@ The code that will be run in the new thread is defined as follows. {{tr|}Nonecopied_argsを参照してみましょう}と<code>t1t2、...、tN、どこNですsizeof...(copied_args)and copied_args is the result of calling decay_copy as defined above. The following code will be run in the new thread:
</code>
  • fがクラスTのメンバ関数へのポインタであるなら、それが呼び出されます。戻り値は無視されます。効果的に、次のコードが実行されます
    Original:
    If f is pointer to a member function of class T, then it is called. The return value is ignored. Effectively, the following code is executed:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • (t1.*f)(t2, ..., tN)t1のタイプはどちらTである場合、またはTTから派生した型への参照の.
    Original:
    (t1.*f)(t2, ..., tN) if the type of t1 is either T, reference to T or reference to type derived from T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • ((*t1).*f)(t2, ..., tN)その他のとき.
    Original:
    ((*t1).*f)(t2, ..., tN) otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • N == 1場合とf、それはクラスのメンバデータオブジェクトへのポインタにアクセスされています。オブジェクトの値は無視されます。効果的に、次のコードが実行されます
    Original:
    If N == 1 and f is pointer to a member data object of a class, then it is accessed. The value of the object is ignored. Effectively, the following code is executed:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • t1.*fいただくとt1のタイプはどちらTですTまたはTから派生した型への参照の.
    Original:
    t1.*f if and the type of t1 is either T, reference to T or reference to type derived from T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • (*t1).*fその他のとき.
    Original:
    (*t1).*f otherwise.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • f他のすべてのケースで非メンバ関数へのポインタと呼ばれている。戻り値は無視されます。効果的に、f(t1, t2, ..., tN)が実行されます.
    Original:
    f is called as a pointer to a non-member function in all other cases. The return value is ignored. Effectively, f(t1, t2, ..., tN) is executed.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
コピーコンストラクタが削除され、スレッドがコピー可能ではありません。いいえ2std::threadオブジェクトが同じスレッドの実行を表すことができない.
Original:
The copy constructor is deleted; threads are not copyable. No two std::thread objects may represent the same thread of execution.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] パラメータ

other -
別のスレッドオブジェクトは、このスレッドオブジェクトを構築します
Original:
another thread object to construct this thread object with
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
f -
新しいスレッドで実行する関数
Original:
function to execute in the new thread
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
args... -
引数は、新しい関数に渡す
Original:
arguments to pass to the new function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 例外

1-2)
noexcept specification:  
noexcept
  (C++11およびそれ以降)
3)
std::system_errorスレッドが開始できなかった場合。例外は、エラー条件std::errc::resource_unavailable_try_againまたは別の実装固有のエラー状態を表すかもしれません.
Original:
std::system_error if the thread could not be started. The exception may represent the error condition std::errc::resource_unavailable_try_again or another implementation-specific error condition.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] ノート

スレッド関数への引数は値でコピーされます。参照引数はスレッド関数に渡す必要があるなら、それは(例えばstd::refまたはstd::crefで)ラップする必要があり.
Original:
The arguments to the thread function are copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数からの戻り値は無視されます。関数が例外をスローした場合、std::terminateが呼び出されます。呼び出し元のスレッドに戻り値や例外を渡すためには、std::promiseまたはstd::asyncを使用することができる.
Original:
Any return value from the function is ignored. If the function throws an exception, std::terminate is called. In order to pass return values or exceptions back to the calling thread, std::promise or std::async may be used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
 
void f1(int n)
{
    for(int i=0; i<5; ++i) {
        std::cout << "Thread " << n << " executing\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));
    }
}
 
int main()
{
    int n = 0;
    std::thread t1; // t1 is not a thread
    std::thread t2(f1, n+1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << '\n';
}

Possible output:

Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Thread 1 executing
Thread 2 executing
Final value of n is 5