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 id
  • using 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. The enable_if_t/is_same_v/remove_cvref_t situation up there is literally just quietly making sure you can't hand it a std::jthread as the callable. Also, per the details below, the std::stop_source member 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 the id, const, noexcept, no drama.
  • stop_source get_stop_source () noexcept β€” gives you the stop_source, noexcept.
  • stop_token get_stop_token () const noexcept β€” gives you the stop_token, const, noexcept.
  • void join () β€” the join.
  • bool joinable () const noexcept β€” are we joinable? Returns a bool, 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 a bool, noexcept.
  • void swap (jthread &__other) noexcept β€” swaps with __other, noexcept.

🌈 Static Public Member Functions 🌈

  • static unsigned hardware_concurrency () noexcept β€” static, returns unsigned, noexcept.

πŸ¦‹ Friends πŸ¦‹

  • void swap (jthread &__lhs, jthread &__rhs) noexcept β€” the free-function friend swap, because sometimes you need to swap two jthreads 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