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_SOURCE at the top like it's a personality trait 😭 you literally need it, bestie, go read feature_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 β€” thread had 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 β€” abstime value is invalid (tv_sec is less than 0 or tv_nsec is greater than 1e9). bestie that timespec is giving "illegal" βœ‹
  • ETIMEDOUT β€” The call timed out before thread terminated. 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