std::jthread
std::jthread (C++20) is a thread whose destructor joins. It also carries a std::stop_token so you can ask the worker to stop cooperatively. Prefer it over a raw std::thread whenever the toolchain has C++20.
Detailed Explanation
std::thread's destructor calls terminate if you forget join. That makes every early return and every exception a land mine. jthread joins in the destructor. RAII applies to the thread handle: leaving the scope waits for the worker. If you need to detach, you still can (detach()), and then the destructor does not join.
The second feature is cooperative cancellation. The constructor can pass a std::stop_token as the first argument of your function if the function's first parameter is stop_token. Inside the function you poll token.stop_requested() or use a condition_variable_any that waits on the token. j.request_stop() sets the stop state. It does not abort the thread. There is no safe way to kill a C++ thread from the outside; stop tokens are the standard "please finish" flag.
jthread is still move-only. join, detach, joinable, get_id match thread. get_stop_source() and get_stop_token() expose the shared stop state. Several jthreads do not share a token unless you pass the same stop_source in.
Destruction order: the destructor requests stop (as if request_stop()) and then joins. A worker that never looks at the token will still run to completion; the destructor just waits. A worker in a tight loop that ignores the token cannot be cancelled. Design the loop to check.
If you need the result of the function, jthread does not provide it. Use std::async or a promise. jthread is about lifetime and stop, not about values.
Compiler support: GCC 10 libstdc++, Clang 16+ with a recent libc++, MSVC 19.28+. Link the pthread library on POSIX (-pthread).
Do not wrap every two-line parallel map in a jthread. The join at the end is a synchronisation point; spawning a thread for a 1 µs body is slower than doing the work. Use threads for work that dwarfs the spawn cost, or for waiting on I/O.
Code Examples
#include <chrono>
#include <iostream>
#include <stop_token>
#include <thread>
void worker(std::stop_token tok) {
while (!tok.stop_requested()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
int main() {
{
std::jthread t(worker); // constructor starts worker
} // destructor request_stop + join
std::cout << "joined\n"; // prints: joined
}#include <iostream>
#include <thread>
#include <vector>
int main() {
std::vector<int> xs(4, 0);
{
std::jthread t([&](std::stop_token) {
xs[0] = 1;
});
}
std::cout << xs[0] << '\n'; // prints: 1
}Common Gotchas
Stop is cooperative
`request_stop` does not unwind the worker. A blocking `recv` without a timeout will ignore the token until the call returns.
Capturing locals and letting `jthread` outlive them
the destructor waits, so a local `jthread` is safe. A `jthread` member that outlives the objects it captured is not.
The destructor always `request_stop`s
a worker that treats stop as "abort the transaction" will see a stop when the scope ends normally too. Structure the function so stop means "leave the loop".
First parameter magic
the `stop_token` is injected only if the callable accepts it as the first argument. A lambda `[]{ ... }` will not receive one.