pthread_tryjoin_np(3)
π pthread_tryjoin_np β bestie, let's try to join a thread π§΅β¨
okay so real talk: this page is from the Library Functions Manual, section 3, and it's all about the two functions that are like pthread_join(3) but with β¨plot twistsβ¨. One of them doesn't wait at all, and the other one has a deadline. Iconic behavior honestly.
πΈ NAME πΈ
pthread_tryjoin_np, pthread_timedjoin_np β try to join with a terminated thread π«Ά
π LIBRARY π
POSIX threads library β you're linking libpthread, so that's -lpthread babes π
β¨ SYNOPSIS β¨
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <pthread.h>
int pthread_tryjoin_np(pthread_t thread, void **retval);
int pthread_timedjoin_np(pthread_t thread, void **retval,
const struct timespec *abstime);
not me casually dropping_GNU_SOURCEat the top like it's a personality trait π you literally need it, bestie, go readfeature_test_macros(7)if you wanna know why.
π DESCRIPTION π
So these two cuties work exactly like pthread_join(3) β same energy, same vibe β except for the differences we're about to spill on this very page. That's it. That's the whole deal. π
pthread_tryjoin_np()</p> does a nonblocking join with thread, and hands you the thread's exit status in *retval. The key thing: if thread hasn't terminated yet, it does NOT sit there blocking like pthread_join(3) would. Nope. It just returns an error and moves on with its life. Main character energy, honestly. π
pthread_timedjoin_np()</p> is the join-with-timeout girlie. If thread hasn't terminated yet, the call blocks β but only for a maximum time, which you specify in abstime, measured against the CLOCK_REALTIME clock. If the timeout expires before thread terminates, the call returns an error. The abstime argument is a timespec(3) structure, and it specifies an absolute time measured since the Epoch (go peek at time(2) for that). β°
π RETURN VALUE π
On success, these functions return 0. On error, they return an error number. no cap, that's the whole story π
π₯ ERRORS π₯
These functions can fail with the same errors as pthread_join(3). But wait β there's more π
pthread_tryjoin_np() can in addition fail with:
EBUSYβthreadhad not yet terminated at the time of the call. she said "not now, I'm busy" π
pthread_timedjoin_np() can in addition fail with:
EINVALβabstimevalue is invalid (tv_secis less than0ortv_nsecis greater than1e9). bestie that timespec is giving "illegal" βETIMEDOUTβ The call timed out beforethreadterminated. the clock ran out, it's over π
and one more lil fact for you: pthread_timedjoin_np() never returns the error EINTR. never ever. periodt. π
π ATTRIBUTES π
For an explanation of the terms used in this section, go see attributes(7) β it's giving homework but it's short, I promise π
pthread_tryjoin_np(),pthread_timedjoin_np()β Thread safety: MT-Safe π
π¦ STANDARDS π¦
GNU β and that's why the names have the suffix _np in them, which stands for nonportable. so like, it's cute but it does not travel βοΈπ
β HISTORY β
glibc 2.3.3. β an elder. a legend. we stan a vintage icon π΅β¨
π BUGS π
okay listen up, this part is important, do not skip it:
The pthread_timedjoin_np() function measures time by internally calculating a relative sleep interval that is then measured against the CLOCK_MONOTONIC clock instead of the CLOCK_REALTIME clock.
Consequently, the timeout is unaffected by discontinuous changes to the CLOCK_REALTIME clock. so yeah, plot twist: you asked for CLOCK_REALTIME but the timeout math is lowkey mono-time. the more you know π
π§ EXAMPLES π§
The following code waits to join for up to 5 seconds β here's the moment you've all been waiting for:
struct timespec ts;
int s;
...
if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
/* Handle error */
}
ts.tv_sec += 5;
s = pthread_timedjoin_np(thread, NULL, &ts);
if (s != 0) {
/* Handle error */
}
see? five seconds. grab the time, add 5, send it. slay π₯
π SEE ALSO π
and that's the whole page bestie!! this was brought to you by Linux man-pages 6.20, dated 2025-05-17, and the original title line was pthread_tryjoin_np(3) from the Library Functions Manual β mwah, go join some 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