pthread_join() and pthread_exit()

Allan Jiang picture Allan Jiang · Dec 15, 2011 · Viewed 113.2k times · Source

I have a question about C concurrency programming.

In the pthread library, the prototype of pthread_join is

int pthread_join(pthread_t tid, void **ret);

and the prototype of pthread_exit is:

void pthread_exit(void *ret);

So I am confused that, why pthread_join takes the return value of the process as a pointer to a void pointer from reaped thread, but pthread_exit only takes a void pointer from the exited thread? I mean basically they are all return values from a thread, why there is a difference in type?

Answer

stefanB picture stefanB · Dec 15, 2011

In pthread_exit, ret is an input parameter. You are simply passing the address of a variable to the function.

In pthread_join, ret is an output parameter. You get back a value from the function. Such value can, for example, be set to NULL.

Long explanation:

In pthread_join, you get back the address passed to pthread_exit by the finished thread. If you pass just a plain pointer, it is passed by value so you can't change where it is pointing to. To be able to change the value of the pointer passed to pthread_join, it must be passed as a pointer itself, that is, a pointer to a pointer.