0

I would like to know if a pthread_t has ever been used. Is there a way to check and see if it is valid before I call pthread_join?

This example code segfaults:

#include <pthread.h>

static pthread_t pth_freq_loop;

main()
{
    pthread_join(pth_freq_loop, NULL);
}

And it can be run as follows:

# gcc -lpthread -o pthread pthread.c 
# ./pthread 
Segmentation fault

This is a duplicate of this thread:

Is there an invalid pthread_t id?

KJ7LNW
  • 1,437
  • 5
  • 11
  • You can always use a separate variable: `static bool pth_freq_loop_is_valid = false;` Set it `true` after a successful `pthread_create`, and check it before calling `pthread_join`. – user3386109 Oct 12 '21 at 01:43
  • @user3386109: Certainly, but is there a pthread-way to do it? – KJ7LNW Oct 12 '21 at 02:43
  • Not that I'm aware of. – user3386109 Oct 12 '21 at 04:41
  • 2
    https://stackoverflow.com/questions/6276939/is-there-an-invalid-pthread-t-id – CristiFati Oct 12 '21 at 11:36
  • 2
    Per the POSIX Rationale document: "**It is the application's responsibility to use only valid thread IDs and to keep track of the lifetime of the underlying threads.**" (https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html) – John Bollinger Oct 12 '21 at 17:29

0 Answers0