Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the second argument to pthread_join() is a **, a pointer to a pointer?

I am new to using pthread and also not that familiar with pointers to pointers. Could somebody perhaps explain why the second argument of pthread_join() is a void **. Why is it designed like this.

int pthread_join(pthread_t thread, void **value_ptr);
like image 269
artic sol Avatar asked Nov 10 '17 11:11

artic sol


People also ask

What is the second argument in pthread_join?

The second argument is a void pointer. pthread_join(pthread_t tid, void * return_value); If the return_value pointer is non-NULL, pthread_join will place at the memory location pointed to by return_value , the value passed by the thread tid through the pthread_exit call.

When you call pthread_join () What happens?

After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated. Joining with a thread that has previously been joined results in undefined behavior. Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".

What is the use of pthread_join () function?

The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.

What is the use of pthread_join () and pthread_exit () function?

The pthread_exit() function terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to another thread in the same process that calls pthread_join(3).


1 Answers

To return a value via a function's argument you need to pass in the address of the variable to receive the new value.

As pthread_join() is designed to receive the pointer value being passed to pthread_exit(), which is a void*, pthread_join() expects the address of a void* which in fact is of type void**.

Example:

#include <stdlib.h> /* for EXIT_xxx macros */
#include <stdio.h> /* for printf() and perror() */
#include <pthread.h> 

void * tf(void * pv)
{
  int * a = pv;
  size_t index = 0;

  printf("tf(): a[%zu] = %d\n", index , a[index]);

  ++index;

  pthread_exit(a + index); /* Return from tf() the address of a's 2nd element. 
                          a + 1 here is equivalent to &a[1]. */
}


int main(void)
{
  int a[2] = {42, 43};
  pthread_t pt;
  int result = pthread_create(&pt, NULL, tf, a); /* Pass to tf() the address of 
                                                    a's 1st element. a decays to 
                                                    something equivalent to &a[0]. */
  if (0 != result)
  {
    perror("pthread_create() failed");
    exit(EXIT_FAILURE);
  }

  {
    int * pi;
    size_t index = 0;

    {
      void * pv;
      result = pthread_join(pt, &pv); /* Pass in the address of a pointer-variable 
                                         pointing to where the value passed to 
                                         pthread_exit() should be written. */
      if (0 != result) 
      {
        perror("pthread_join() failed");
        exit(EXIT_FAILURE);
      }

      pi = pv;
    }

    ++index;

    printf("main(): a[%zu] = %d\n", index, pi[0]);
  }

  return EXIT_SUCCESS;
}

The program above is expected to print:

tf(): a[0] = 42
main(): a[1] = 43
like image 109
alk Avatar answered Sep 23 '22 18:09

alk