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?
pthread_exit is called from the thread itself to terminate its execution (and return a result) early. pthread_join is called from another thread (usually the thread that created it) to wait for a thread to terminate and obtain its return value.
On return from a successful pthread_join() call with a non-NULL value_ptr argument, the value passed to pthread_exit() by the terminating thread shall be made available in the location referenced by value_ptr. When a pthread_join() returns successfully, the target thread has been terminated.
The pthread_exit() function terminates the calling thread and makes the value value_ptr available to any successful join with the terminating thread. Any cancellation cleanup handlers that have been pushed and not yet popped are popped in the reverse order that they were pushed and then executed.
The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.
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.
It because every time
void pthread_exit(void *ret);
will be called from thread function so which ever you want to return simply its pointer pass with pthread_exit().
Now at
int pthread_join(pthread_t tid, void **ret);
will be always called from where thread is created so here to accept that returned pointer you need double pointer ..
i think this code will help you to understand this
#include <stdio.h> #include <string.h> #include <pthread.h> #include <stdlib.h> void* thread_function(void *ignoredInThisExample) { char *a = malloc(10); strcpy(a,"hello world"); pthread_exit((void*)a); } int main() { pthread_t thread_id; char *b; pthread_create (&thread_id, NULL,&thread_function, NULL); pthread_join(thread_id,(void**)&b); //here we are reciving one pointer value so to use that we need double pointer printf("b is %s\n",b); free(b); // lets free the memory }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With