If you're doing anything with a pthread_t
other than passing it to a function that takes one, you're doing something wrong. Only the pthreads API knows how to use a pthread_t
correctly. They can have any internal structure that's convenient for the implementation.
Being a C language construct, pthread_t
behaves more like char *
. The necessary language constructs to make it behave like std::string
don't exist. So you have to treat it like char *
.
A char *
contains a string somehow, but you have to understand its implementation to get that value out. Consider:
char *j = "hello";
char *k = strdup (j);
if (j == k)
printf ("This won't happen\n");
printf ("%d\n", j);
printf ("%d\n", k); // these won't be equal
You can't compare char *
's with ==
to see if they refer to the same string. And if you print out j
and k
, you'll get different values.
Similarly, a pthread_t
refers to one particular thread somehow. But you have to understand how to get the value out. Two pthread_t
s can have different apparent values but still refer to the same thread just as two char *
s can have different apparent values but still refer to the same string.
Just as you compare two char *
's with strcmp
if you want to tell if they refer to the same string value, you compare two pthread_t
s with pthread_equal
to tell if they refer to the same thread.
So this line of code makes no sense:
printf("FLAG = %d ", thread_id);
A pthread_t
is not an integer and you can't print it with a %d
format specifier. POSIX has no printable thread IDs. If you want one, you need to code one (perhaps using pthread_getspecific
).