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 );
thread( const thread& ) = delete;
(4) (since C++11)Constructs a new std::thread
object.
1) Creates a new std::thread
object which does not represent a thread.
2) Move constructor. Constructs the std::thread
object to represent the thread of execution that was represented by other. After this call other no longer represents a thread of execution.
Creates a new
std::thread
object and associates it with a thread of execution. The new thread of execution starts executing:
The calls of decay-copy are evaluated(until C++23)The values produced by auto are materialized(since C++23)in the current thread, so that any exceptions thrown during evaluation and copying/moving of the arguments are thrown in the current thread, without starting the new thread.
This overload participates in overload resolution only if
std::decay<F>::type(until C++20)std::remove_cvref_t<F>(since C++20)is not the same type as
std::thread
.
The completion of the invocation of the constructor
synchronizes withthe beginning of the invocation of the copy of
fon the new thread of execution.
4) The copy constructor is deleted; threads are not copyable. No two std::thread
objects may represent the same thread of execution.
equal to
std::thread::id()and
get_id()returns the value of
other.get_id()prior to the start of construction.
[edit] Exceptions 3) std::system_errorif 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.
[edit] NotesThe 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., with std::ref or std::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 or std::async may be used.
[edit] ExamplePossible output:
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[edit] Defect reports
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 be std::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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4