1
int pthread_join(pthread_t thread, void **retval);

According to the man page pthread_join should use a pointer to a pointer as argument to store the return value.I cant understand why its designed that way.Is it sufficient to use a pointer variable in that ?

Fazil Uruniyengal
  • 286
  • 1
  • 3
  • 15
  • Just because you don't understand it doesn't mean it will work if you don't follow the function's spec. – Scott Hunter Dec 01 '14 at 16:31
  • It receives the pointer you returned from your thread proc (which has a `void*` result). That value has to be stored *somewhere*. The `pthread_join` result would be a candidate, but it is used to convey the function success/failure result. Thus, an out-param is born. As an out-param in C, it provides the target as an address, thus the pointer-to-pointer. – WhozCraig Dec 01 '14 at 16:31
  • 1
    It's designed that way so that the thread can store a void* somewhere that is not going to go away when the thread does. – Martin James Dec 01 '14 at 16:32

2 Answers2

2

If I have understood your query clearly.. you should use like this..

pthread_t a_thread;
void *thread_result;
pthread_join(a_thread, &thread_result);
µtex
  • 900
  • 5
  • 12
  • The bit missing is that the return value *is* a `void *`; it need not actually point to anything as it is an opaque pointer. – abligh Dec 01 '14 at 17:48
1

The start routine you pass to pthread_create returns a value of type Foobar. I don't know what Foobar is, but if you want to capture that value in pthread_join, you have to pass a Foobar* in.

Now when I look at pthread_create documentation, I see that Foobar is actually void*. Hence pthread_join should accept void**.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243