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
returnfrom the start function of any thread that isn't the main thread, that results in an implicit call topthread_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 thanexit(3). Big difference. Choose wisely ๐ - The value pointed to by
retvalshould 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