pthread_detach(3)
β¨ NAME β¨
pthread_detach β detach a thread π§΅π
okay so we're in the pthread_detach(3) part of the manual, bestie, and this one is genuinely so useful it's not even funny.
π LIBRARY π
This lives in the POSIX threads library β that's libpthread, and you link it with -lpthread. π«Ά
π SYNOPSIS π
#include <pthread.h>
int pthread_detach(pthread_t thread);
π« DESCRIPTION π«
So pthread_detach() is the function that marks the thread identified by thread as detached. that's it, that's the whole vibe. β¨
And when a detached thread terminates, its resources are automatically released back to the system β no need for another thread to come along and join with the terminated thread. it's giving self-cleaning, it's giving low-maintenance queen. π
real talk though: attempting to detach an already detached thread results in unspecified behavior. don't do it. the standard literally will not tell you what happens, and that's terrifying fr fr. π
π RETURN VALUE π
- On success,
pthread_detach()returns0β - On error, it returns an error number π
π₯ ERRORS π₯
EINVALβthreadis not a joinable thread. nope, wrong kind of thread, sorry bestie πESRCHβ No thread with the IDthreadcould be found. it's not there! nowhere! gone! π
π¦ ATTRIBUTES π¦
For an explanation of the terms used in this section, see attributes(7). (iykyk πβ¨)
- Interface:
pthread_detach() - Attribute: Thread safety
- Value: MT-Safe
so it's MT-Safe, meaning go off, call it from multiple threads, she's fine. π«Ά
π STANDARDS π
POSIX.1-2008. certified, official, iconic.
πΈ HISTORY πΈ
POSIX.1-2001. yeah she's been around, an OG. π
π NOTES π
- Once a thread has been detached, it can't be joined with
pthread_join(3)or be made joinable again. that door is CLOSED, bestie. periodt. πͺπ - A new thread can be created in a detached state using
pthread_attr_setdetachstate(3)to set the detached attribute of theattrargument ofpthread_create(3). so you can have it detached from day one, main character energy from birth. β¨ - The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using
exit(3)β or equivalently, if the main thread returns. so like, it's not a shield, it's a cleanup policy. important distinction. π - Either
pthread_join(3)orpthread_detach()should be called for each thread that an application creates, so that system resources for the thread can be released. basically pick one, don't ghost your threads. π
but note that the resources of any threads for which one of these actions has not been done will be freed when the process terminates. so like⦠it works out eventually, but don't test it.
π§ EXAMPLES π§
The following statement detaches the calling thread:
pthread_detach(pthread_self());
look at her. detaching herself. self-care. we love to see it. π«Άβ¨
π SEE ALSO π
pthread_attr_setdetachstate(3)pthread_cancel(3)pthread_create(3)pthread_exit(3)pthread_join(3)pthreads(7)
and that's the whole page, bestie!! π this came from Linux man-pages 6.20, dated 2025-05-17 β go read the other pthread pages, ily, stay joined (unless you detach, which is also valid). ππ¦β¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms