std::jthread(3cxx)
π std::jthread, from the cxx manual π
Okay bestie, sit down, hydrate, because this one is genuinely iconic. This is the cxx manual's page for std::jthread β and yes, it's the C++ girlie, but the vibes are immaculate and the facts are all here, no cap. π
β¨ NAME β¨
std::jthread β the thread that actually has your back π«Ά
πΈ SYNOPSIS πΈ
#include <thread>
π Public Types π
using idusing native_handle_type
π Public Member Functions π
Listen, there are a lot of these and we are listing every single one, periodt. No skipping, no summarizing, we're not that kind of girl.
template<typename _Callable, typename... _Args, typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>, jthread>>> jthread (_Callable &&__f, _Args &&... __args)β the constructor that does the whole main character thing: give it a callable and some args and it makes the thread. Theenable_if_t/is_same_v/remove_cvref_tsituation up there is literally just quietly making sure you can't hand it astd::jthreadas the callable. Also, per the details below, thestd::stop_sourcemember gets passed in as the first argument to your callable β but only as long as your callable will accept that argument. β¨jthread (const jthread &)=deleteβ copy constructor? Deleted. Not invited. π«jthread (jthread &&) noexcept=defaultβ move constructor,noexcept, defaulted. She moves, she doesn't copy.void detach ()β the detach.id get_id () const noexceptβ hands you theid,const,noexcept, no drama.stop_source get_stop_source () noexceptβ gives you thestop_source,noexcept.stop_token get_stop_token () const noexceptβ gives you thestop_token,const,noexcept.void join ()β the join.bool joinable () const noexceptβ are we joinable? Returns abool,const,noexcept.native_handle_type native_handle ()β the raw native handle, for when you need to get real close to the platform.jthread & operator= (const jthread &)=deleteβ copy assignment? Deleted. Same energy as above. πjthread & operator= (jthread &&__other) noexceptβ move assignment,noexcept.bool request_stop () noexceptβ requests a stop, gives you back abool,noexcept.void swap (jthread &__other) noexceptβ swaps with__other,noexcept.
π Static Public Member Functions π
static unsigned hardware_concurrency () noexceptβ static, returnsunsigned,noexcept.
π¦ Friends π¦
void swap (jthread &__lhs, jthread &__rhs) noexceptβ the free-function friend swap, because sometimes you need to swap twojthreads from the outside,noexcept.
π DETAILED DESCRIPTION π
A thread with cancellation and automatic joining. That's the whole thesis bestie, but let's unpack because it's so good. π
Unlike std::thread, destroying a joinable std::jthread will not terminate the process. Nope, not today. Instead, it will try to request its thread to stop, then will join it. That's the automatic joining. That's the cancellation. That's the glow-up.
A std::jthread has a std::stop_source member which will be passed as the first argument to the callable that runs in the new thread β as long as the callable will accept that argument. That can then be used to send a stop request that the new thread can test for. So the thread can literally be like "hey, are we stopping?" and you can be like "yes babe, stop". π₯
Fr fr, don't sleep on that "will not terminate the process" line. It's the entire reason this class exists and it's lowkey the best part.
β Since β
C++20. Our era. π
π§ AUTHOR π§
Generated automatically by Doxygen for libstdc++ from the source code. Not a human, just a little robot doing its best. π
And that's the page! Sending you off with the source/version deets β this one lives under libstdc++ and the manual name is cxx, so next time you're flipping through the cxx pages and land on std::jthread, you'll know exactly what's up. Slay, stay joinable, and remember: the destructor is a safe queen now. πβ¨π
π¦ where this page comes from
- ubuntu resolute: libstdc++-12-doc 12.5.0-9ubuntu1
- ubuntu resolute: libstdc++-13-doc 13.4.0-10ubuntu1
- ubuntu stonking: libstdc++-12-doc 12.5.0-9ubuntu1
- ubuntu stonking: libstdc++-13-doc 13.5.0-1ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms