I'm attempting to print a return value from pthread_join. I have the following code:
for(j = 0 ; j < i ; ++j){
pthread_join( tid[j], returnValue); /* BLOCK */
printf("%d\n", (int)&&returnValue);
}
All of the threads are stored in the tid array and are created and returned correctly. At the end of each thread function I have the following line:
pthread_exit((void *)buf.st_size);
I am attempting to return the size of some file I was reading. For some reason I cannot get it to print the correct value. It is more then likely the way I am attempting to dereference the void ** from the pthread_join function call, but I'm not too sure how to go about doing that. Thanks in advance for any help.
The pthread_join() function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.
If you want to return only status of the thread (say whether the thread completed what it intended to do) then just use pthread_exit or use a return statement to return the value from the thread function.
Returned value pthread_exit() cannot return to its caller. There are no documented errno values. Use perror() or strerror() to determine the cause of the error.
If successful, pthread_create() returns 0.
You need to pass the address of a void *
variable to pthread_join
-- it will get filled in with the exit value. That void *
then should be cast back to whatever type was originally stored into it by the pthread_exit
call:
for(j = 0 ; j < i ; ++j) {
void *returnValue;
pthread_join( tid[j], &returnValue); /* BLOCK */
printf("%zd\n", (size_t)(off_t)returnValue);
}
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