cpthreadspthread-join

Why is retval a void** in pthread_join?


I am having a hard time understanding why pthread_join's retval argument is a void**. I have read the manpage and tried to wrap my head around it but I still cannot fully understand it. I couldn't convince myself that retval cannot be a void*. Could someone please enlighten me?

Thank you very much in advance!


Solution

  • It's because you are supposed to supply the address of a void* to pthread_join. pthread_join will then write the address supplied by pthread_exit(void*) into the variable (who's address you supplied).

    Example scenario:

    typedef struct {
        // members
    } input_data;
    
    typedef struct {
        // members
    } output_data;
    

    Starting thread side:

    input_data id;
    pthread_create(..., start_routine, &id);
    
    void* start_routine(void *ptr) {
        input_data *id = ptr;
        output_data *od = malloc(sizeof *od);
        // use the input data `id`, populate the output data `od`.
        pthread_exit(od);
    }
    

    Joining side:

    output_data *od;
    pthread_join((void**) &od);
    // use `od`
    free(od);