简体   繁体   中英

C pthread_join return value

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.

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);
}

This is working:

for(j = 0 ; j < i ; ++j) {
    int returnValue;
    pthread_join( tid[j], (void **)&returnValue);  /* BLOCK */
    printf("%d\n",  returnValue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM