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() returns 0 ⭐
  • On error, it returns an error number πŸ™ˆ

πŸ”₯ ERRORS πŸ”₯

  • EINVAL β€” thread is not a joinable thread. nope, wrong kind of thread, sorry bestie πŸ’…
  • ESRCH β€” No thread with the ID thread could 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 the attr argument of pthread_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) or pthread_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 πŸ’œ


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