std::jthread::detach(3)
π 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 π₯
joinableisfalse.
That's it. That's the postcondition. No cap, the thread is no longer joinable after this, periodt π¦
π Exceptions π
std::system_errorifjoinable() == falseor an error occurs.
Careful bestie!! If it's not joinable and you calldetachanyway, it throwsstd::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 forjoinablein 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms