pthread_exit(3)

๐Ÿ’– pthread_exit(3) โ€” bestie edition ๐Ÿ’–

okay so you wanna know about pthread_exit? Welcome to the pthread_exit(3) page, babes โ€” it's a library function, part of the POSIX threads library situation, and it's lowkey one of the most iconic ways to dip out of a thread. Let's get into it ๐Ÿซถ


โœจ NAME โœจ

pthread_exit โ€” terminate the calling thread ๐Ÿ’…

๐Ÿ“š LIBRARY ๐Ÿ“š

POSIX threads library โ€” that's libpthread, and you link it with -lpthread.

๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <pthread.h>

[[noreturn]] void pthread_exit(void *retval);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so here's the tea. pthread_exit() terminates the calling thread, and it hands a value back through retval โ€” and if that thread is joinable, that value is available to another thread in the same process that goes and calls pthread_join(3). it's giving "leaving a little note behind for whoever comes looking" โœจ

Any clean-up handlers you set up with pthread_cleanup_push(3) that haven't been popped yet? They get popped and executed, and they go in the reverse of the order in which they were pushed. LIFO forever, no cap. ๐ŸŽ€

And if the thread has any thread-specific data? Then, after all those clean-up handlers have run, the corresponding destructor functions get called, in an unspecified order. (unspecified order = don't @ me about which one goes first, okay ๐Ÿ˜ญ)

When a thread terminates, listen โ€” process-shared resources are not released. Like, at all. That means stuff like mutexes, condition variables, semaphores, and file descriptors are just... still there. And functions you registered using atexit(3) are not called. Yeah. Not called. ๐Ÿ’€

BUT โ€” and this is the plot twist โ€” after the last thread in a process terminates, the process itself terminates, as by calling exit(3) with an exit status of zero. So at that point process-shared resources are released and functions registered using atexit(3) are called. The finale hits different ๐Ÿ‘‘

๐ŸŽฏ RETURN VALUE ๐ŸŽฏ

This function does not return to the caller. It just... doesn't come back. It's on its own journey now ๐Ÿงš

๐Ÿ’€ ERRORS ๐Ÿ’€

This function always succeeds. Like, always. Iconic behavior honestly.

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

For an explanation of the terms used in this section, go check out attributes(7).

  • Interface: pthread_exit()
    • Attribute: Thread safety
    • Value: MT-Safe โœ…

๐Ÿ“œ STANDARDS ๐Ÿ“œ

POSIX.1-2008. periodt.

๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

POSIX.1-2001. yes bestie, the lore goes back that far ๐ŸŒˆ

๐Ÿ“ NOTES ๐Ÿ“

  • If you do a return from the start function of any thread that isn't the main thread, that results in an implicit call to pthread_exit(), and it uses the function's return value as the thread's exit status.
  • If you want the other threads to keep on doing their thing, the main thread should terminate by calling pthread_exit() rather than exit(3). Big difference. Choose wisely ๐Ÿ’…
  • The value pointed to by retval should not live on the calling thread's stack, because the contents of that stack are undefined after the thread terminates. So like, don't point at something that's about to evaporate, bestie ๐Ÿ™ˆ

๐Ÿ› BUGS ๐Ÿ›

Currently, there are limitations in the kernel implementation logic for wait(2)ing on a stopped thread group with a dead thread group leader. This can manifest in problems such as a locked terminal if a stop signal is sent to a foreground process whose thread group leader has already called pthread_exit(). Yeah, that's a real one. Not cute, but real. ๐Ÿ˜ญ

๐Ÿ”— SEE ALSO ๐Ÿ”—

pthread_create(3), pthread_join(3), pthreads(7)


and that's the whole page, bestie ๐Ÿซถ this was Linux man-pages 6.20, dated 2025-05-17, signing off from pthread_exit(3) โ€” go forth and exit your threads responsibly ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ 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