Ankur Agarwal
Ankur Agarwal

Reputation: 24768

How is exit status passed between pthread_exit and pthread_join? Is a correction needed in man page?

Question:

How exactly is exit status passed between pthread_exit and pthread_join?

From pthread_join man page

   int pthread_join(pthread_t thread, void **retval);

If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval. If the target thread was canceled, then PTHREAD_CANCELED is placed in *retval.

I think the wording in the man page is incorrect.

It should be "If retval is not NULL, then pthread_join() copies the address of the variable holding the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by retval."

I wrote this code that shows this, see code comments:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void * function(void*);

int main()
{
    pthread_t thread;
    int arg = 991;
    int * status; // notice I did not intialize status, status is *retval
    pthread_create(&thread, NULL, function, (void*)(&arg));

    pthread_join(thread, (void **)(&status));//passing address of status,&status is retval 

    //(2) this address is same as printed in (1)
    printf("The address of returned status is %p,", status); 

    printf("The returned status is %d\n", *status);
}

void * function(void * arg)
{
    int *p;
    p = (int*)arg;
    printf("I am in thread.\n");

     //(1) printing the address of variable holding the exit status of thread, see (2)                                                              
    printf("The arg address is %p %p\n", p, arg); 

    pthread_exit(arg);
}

Sample o/p:

I am in thread.

The arg address is 0xbfa64878 0xbfa64878

The address of returned status is 0xbfa64878,The returned status is 991***

Upvotes: 2

Views: 5614

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171333

Your code doesn't contradict the man page.

If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval.

You call pthread_join with retval=&status, so it's not NULL.

You called pthread_exit(0xbfa64878) so the exit status of the target thread is 0xbfa64878 and that gets copied into *retval i.e. status = 0xbfa64878, which is what you print out.

I think you're confusing things with labels such as "address of returned status" and "arg address" ... you're giving labels to the values which pthreads doesn't imply. What the man page says is that *retval gets set to the value passed to pthread_exit and that's what your test shows.

In your proposed change:

If retval is not NULL, then pthread_join() copies the address of the variable holding the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by retval.

What is "the variable holding the exit status of the target thread"? Pthreads defines no such thing. The exit status of the target thread is the value passed to pthread_exit, it's not the value of some other variable.

Upvotes: 5

Related Questions