From the manpage:
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.
Let's look at the signature of pthread_exit
.
noreturn void pthread_exit(void *retval);
So that means if we wanted to return an int
from our thread it would look something like this:
void* foo() {
// ...
int value = 255;
pthread_exit(&value);
}
This works because the compiler doesn't care that it's an int*
or a void*
, either way it's a pointer of the same size.
Now we want to actually extract the return value of the thread using pthread_join
.
void bar() {
pthread_t thread_id;
int *returnValue;
// create thread etc...
// the original type of returnValue was an `int*` so when we pass it in
// with "&" it's now become `int**`
pthread_join(thread_id, &returnValue);
printf("%d\n", *returnValue); // should print 255
}
In plain English pthread_join
takes a pointer and sets it address to point at the retval
from your thread. It's a void**
because we need the address of the pointer to be able to set the underlying pointer to what we want.