|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
thread()noexcept; | (1) | (since C++11) |
thread( thread&& other)noexcept; | (2) | (since C++11) |
template<class F,class...Args> explicit thread( F&& f, Args&&...args); | (3) | (since C++11) |
thread(const thread&)= delete; | (4) | (since C++11) |
Constructs a newstd::thread object.
std::thread object which does not represent a thread.std::thread object to represent the thread of execution that was represented byother. After this callother no longer represents a thread of execution.std::thread object and associates it with a thread of execution. The new thread of execution starts executing:INVOKE(decay-copy(std::forward<F>(f)), | (until C++23) |
std::invoke(auto(std::forward<F>(f)), | (since C++23) |
std::thread.If any of the following conditions is satisfied, the program is ill-formed:
| (until C++20) |
If any of the following isfalse, the program is ill-formed:
| (since C++20) |
std::thread objects may represent the same thread of execution.Contents |
| other | - | another thread object to construct this thread object with |
| f | - | Callable object to execute in the new thread |
| args | - | arguments to pass to the new function |
std::errc::resource_unavailable_try_again or another implementation-specific error condition.The arguments to the thread function are moved or copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g., withstd::ref orstd::cref).
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 orstd::async may be used.
#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 is not a threadstd::thread t2(f1, n+1);// pass by valuestd::thread t3(f2,std::ref(n));// pass by referencestd::thread t4(std::move(t3));// t4 is now running f2(). t3 is no longer a threadstd::thread t5(&foo::bar,&f);// t5 runs foo::bar() on object fstd::thread t6(b);// t6 runs baz::operator() on a copy of object b 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';}
Possible output:
Thread 1 executingThread 2 executingThread 3 executingThread 4 executingThread 3 executingThread 1 executingThread 2 executingThread 4 executingThread 2 executingThread 3 executingThread 1 executingThread 4 executingThread 3 executingThread 2 executingThread 1 executingThread 4 executingThread 3 executingThread 1 executingThread 2 executingThread 4 executingFinal value of n is 5Final value of f.n (foo::n) is 5Final value of b.n (baz::n) is 0
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2097 | C++11 | for overload(3),F could bestd::thread | F is constrained |
| LWG 3476 | C++20 | overload(3) directly required (the decayed types of)F and the argument types to be move constructible | removed these requirements[1] |
constructs newjthread object(public member function of std::jthread)[edit] | |
C documentation forthrd_create | |