Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C pthread_join return value

Tags:

c

return

pthreads

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.

like image 743
Night Train Avatar asked Nov 09 '12 20:11

Night Train


People also ask

What is pthread_join in C?

The pthread_join() function suspends execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

How do I get my Pthread return value?

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.

Does pthread_exit () return value?

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.

What does pthread_create return when successful?

If successful, pthread_create() returns 0.


1 Answers

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);
}
like image 130
Chris Dodd Avatar answered Sep 30 '22 00:09

Chris Dodd