Search code examples
c++cpthreadspthread-join

Pthreads, confusion with pthread_join(pthread_t, void**)


I can't understand why does pthread_join takes as 2nd argument void** for the return value, whereas pthread_exit, which is given the return value, has the return value argument as void*.


Solution

  • pthread_join waits for the thread to end, and the resulting value from pthread_exit is stored into *value_ptr. If you want to ignore the result, you can pass NULL for the value_ptr. This is the common C practice of simulating pass by reference by passing a pointer to a variable. See Passing by reference in C

    The pthread_join returns 0 as the function return value on success; then you know that the thread has been joined, and you can access the value from *value_ptr.

    void *value = NULL;
    if (pthread_join(thread, &value) == 0) {
        // thread has ended, and the exit value is available in
        // the value variable
    }