std::jthread::jthread
jthread() noexcept;
|
(1) | (since C++20) |
jthread( jthread&& other ) noexcept;
|
(2) | (since C++20) |
template< class F, class... Args >
explicit jthread( F&& f, Args&&... args );
|
(3) | (since C++20) (until C++29) |
template< class... Args >
explicit jthread( Args&&... args );
|
(3) | (since C++29) |
jthread( const jthread& ) = delete;
|
(4) | (since C++20) |
Constructs new std::jthread object.
std::jthread object which does not represent a thread.std::jthread object to represent the thread of execution that was represented by other. After this call other no longer represents a thread of execution.std::jthread object and associates it with a thread of execution.
The new thread of execution starts executing:
std::invoke(decay-copy(std::forward<F>(f)), get_stop_token(),
|
(until C++23) |
|
|
(since C++23) |
if the expression above is well-formed, otherwise starts executing:
std::invoke(decay-copy(std::forward<F>(f)),
|
(until C++23) |
|
|
(since C++23) |
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.std::remove_cvref_t<F> is not the same type as std::jthread(until C++29)These overloads participate in overload resolution only if Args is not an empty pack and std::remove_cvref_t<Args...[0]> is not the same type as std::jthread(since C++29).|
The leading arguments whose decayed types are thread attribute types (std::thread::name_hint, std::thread::stack_size_hint, or an implementation-defined thread attribute type) are consumed as thread attributes and may affect the behavior of the new thread. Let i be the smallest index such that std::decay_t<Args...[i]> is not a thread attribute type; then f denotes args...[i] and args... denotes the arguments that follow it.
|
(since C++29) |
|
If any of the following is false, the program is ill-formed:
|
(since C++20) (until C++29) |
|
If any of the following is
false, the program is ill-formed:
If every argument has a thread attribute type, i.e., the index i above does not exist, the program is ill-formed.
|
(since C++29) |
f on the new thread of execution.std::jthread objects may represent the same thread of execution.Parameters
| attrs | - | thread attribute objects to apply to the new thread (since C++29) |
| other | - | another std::jthread object to construct this std::jthread object with
|
| f | - | Callable object to execute in the new thread |
| args | - | arguments to pass to the new function |
Postconditions
get_id() equal to std::jthread::id() (i.e. joinable() returns false) and get_stop_source().stop_possible() is false.other.get_id() equal to std::jthread::id() and get_id() returns the value of other.get_id() prior to the start of construction.get_id() not equal to std::jthread::id() (i.e. joinable() returns true), and get_stop_source().stop_possible() is true.Exceptions
std::errc::resource_unavailable_try_again or another implementation-specific error condition.Notes
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., 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.
|
The thread attributes are hints: an implementation may ignore them or adjust the requested values. Thread attributes cannot be attached to an existing thread; they only affect the thread created by the constructor they are passed to. |
(since C++29) |
Example
#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
using namespace std::literals;
void f1(int n)
{
for (int i = 0; i < 5; ++i)
{
std::cout << "[1] ";
++n;
std::this_thread::sleep_for(10ms);
}
std::ignore = n;
}
void f2(int& n)
{
for (int i = 0; i < 5; ++i)
{
std::cout << "[2] ";
++n;
std::this_thread::sleep_for(10ms);
}
}
class foo
{
public:
void bar()
{
for (int i = 0; i < 5; ++i)
{
std::cout << "[3] ";
++n;
std::this_thread::sleep_for(10ms);
}
}
int n = 0;
};
class baz
{
public:
void operator()()
{
for (int i = 0; i < 5; ++i)
{
std::cout << "[4] ";
++n;
std::this_thread::sleep_for(10ms);
}
}
int n = 0;
};
int main()
{
int n = 0;
foo f;
baz b;
std::jthread t0; // t0 is not a thread
std::jthread t1(f1, n + 1); // pass by value
std::jthread t2a(f2, std::ref(n)); // pass by reference
std::jthread t2b(std::move(t2a)); // t2b is now running f2(). t2a is no longer a thread
std::jthread t3(&foo::bar, &f); // t3 runs foo::bar() on object f
std::jthread t4(b); // t4 runs baz::operator() on a copy of object b
t1.join();
t2b.join();
t3.join();
std::cout << std::endl;
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';
} // t4 joins on destruction
Possible output:
[1] [2] [4] [3] [1] [3] [2] [4] [1] [3] [4] [2] [1] [3] [2] [4] [1] [3] [2] [4]
Final value of n is 5
Final value of f.n (foo::n) is 5
Final value of b.n (baz::n) is 0
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 3476 | C++20 | overload (3) directly required (the decayed types of)F and the argument types to be move constructible
|
removed these requirements[1] |
- ↑ The move-constructibility is already indirectly required by std::is_constructible_v.
See also
constructs new thread object (public member function of std::thread)
| |
C documentation for thrd_create
| |