std::jthread::detach(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::jthread::detach β€” bestie, it's literally std::jthread::detach, that's the whole name, we said what we said ✨

So this little icon lives in the C++ Standard Libary (yes that's how the manual spells it, no we're not fixing it, we don't do that here 🫢) and it's about politely letting a thread go live its own independent life. Main character energy for the thread, not for you.


🌸 Synopsis 🌸

void detach();  (since C++20)

Okay so here's the vibe check: detach separates the thread of execution from the jthread object, which means execution is allowed to just... keep going, independently, on its own little journey πŸ’«

And the sweet part? Any allocated resources will be freed once the thread exits. So it's not leaking, it's just vibing until it's done, and then the cleanup fairy shows up 🧚

After calling detach, *this no longer owns any thread. Like, zero threads. Not a single one. It's giving "we are not together anymore" but make it mutual and legal 😭


πŸŽ€ Parameters πŸŽ€

(none) β€” nothing, nada, zero arguments, she takes no notes πŸ’…


⭐ Return value ⭐

(none) β€” also nothing! void said "I'm not giving you a thing and that's final" πŸ‘‘


πŸ”₯ Postconditions πŸ”₯

  • joinable is false.

That's it. That's the postcondition. No cap, the thread is no longer joinable after this, periodt πŸ¦‹


πŸ’€ Exceptions πŸ’€

  • std::system_error if joinable() == false or an error occurs.
Careful bestie!! If it's not joinable and you call detach anyway, it throws std::system_error. That's the vibe check failing, and honestly? Fair. Read the warnings, they're there for a reason πŸ™ˆ

✨ Example ✨

// Run this code

#include <chrono>
#include <iostream>
#include <thread>

void independentThread()
{
    std::cout << "Starting concurrent thread.\n";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Exiting concurrent thread.\n";
}

void threadCaller()
{
    std::cout << "Starting thread caller.\n";
    std::jthread t(independentThread);
    t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Exiting thread caller.\n";
}

int main()
{
    threadCaller();
    std::this_thread::sleep_for(std::chrono::seconds(5));
}

Possible output:

Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.

See how the caller exits before the concurrent thread does? That's detach doing its thing β€” the thread kept running after the jthread stopped owning it, and it printed its exit line all on its own. Literally independent queen behavior πŸ’œ


🧚 References 🧚

  • C++23 standard (ISO/IEC 14882:2023):
    • 33.4.4.3 Members [thread.jthread.mem]
  • C++20 standard (ISO/IEC 14882:2020):
    • 32.4.3.2 Members [thread.jthread.mem]

Receipts provided, iykyk πŸ”


🌈 See also 🌈

  • join β€” waits for the thread to finish its execution (public member function). The patient one, the one who stays 🫢
  • joinable β€” checks whether the thread is joinable, i.e. potentially running in parallel (public member function). This is the context for joinable in this page β€” the vibe check function itself πŸ’…
  • C documentation for thrd_detach β€” the C cousin, different font, same energy 🌸

okay that's the whole page, you ate that, go detach some threads responsibly πŸ’–

mwah πŸ’‹ β€” dropped by cppreference.com, dated 2024.06.10, version of record: std::jthread::detach(3). Told you I'd mention it, I always keep my promises πŸŽ€

πŸ“¦ where this page comes from