q0987
q0987

Reputation: 35982

how does pthread_join populate the variable of thread_result

Note: I have removed all required error checking in the following snippet.

...
void *thread_function(void *arg)
{
   ...
   pthread_exit("Hello");
}

pthread_t a_thread;
void *thread_result;

pthread_create(&a_thread, NULL, thread_function, NULL);
pthread_join(a_thread, &thread_result);
/*

int pthread_join(pthread_t th, void **thread_return);
The second argument is a pointer to a pointer that itself points to the return
value from the thread.

int pthread_exit(void *retval);
This function terminates the calling thread, returning a pointer to an object which
cannot be a local variable.

*/

Question: how does pthread_join populate the variable of thread_result? Since the variable thread_result has no allocated space to hold information, if pthread_join allocates space for thread_result, then major thread must deallocate the resource holding by the varable. As you can see, the code doesn't include the deallocation resource of thread_result. So I assume that pthread_join in fact doesn't allocate space for thread_result.

Now the new question is how the variable thread_result can contain information without being allocated any space?

//Update-1: Add the definition of pthread_exit.

//Update-2: Add the definition of thread_function.

Upvotes: 2

Views: 2829

Answers (5)

Antti
Antti

Reputation: 12479

thread_result is simply a pointer to the data that thread_function returns. If thread_function returns an int cast to void *, the thread calling pthread_join must be aware of this and treat thread_result as int. If on the other hand thread_function returns a pointer to allocated memory, the thread calling pthread_join must be aware of this and eventually free the memory.

In your example, where thread_function returns a string literal, thread_result will be a pointer to the string literal. It's the same as this:

 const char *str = "Hello";

String literals are generally allocated in the data section, so you shouldn't free them.

Upvotes: 2

NPE
NPE

Reputation: 500407

Your conclusion is correct: pthread_join doesn't allocate memory for the result.

In fact, what happens is quite simple:

  • pthread_exit is provided a (void*) pointer to the result by the thread itself; it's up to the thread to decide where this pointer comes from.
  • Subsequently, pthread_join -- called from another thread -- stores that pointer in the variable pointed to by its second argument.

As far as the result is concerned, all that pthreads is doing is passing a pointer across thread boundaries. It is up to the application to ensure that the pointed-to memory is deallocated in a manner that's consistent with how it's been allocated.

Upvotes: 3

cnicutar
cnicutar

Reputation: 182649

Well, pthread_join doesn't allocate anything. You've got your thread function

void *thread_fun(void *arg)
{
    /* stuff */


    return something;
}

Then pthread_join comes along, and before it returns:

if (NULL != value_ptr) {
    *value_ptr = return_value; /* What you returned from your function. */
}

So the thread function must allocate stuff.

Upvotes: 2

user482594
user482594

Reputation: 17486

Well, thread_result appears to be declared as a pointer. Pointer does not really need a allocated space to hold information. The pointer will be pointed to the memory address returned from the pthread_join.

And more importantly, you have to malloc the result that you are going to return at the end of the thread_function, otherwise, heap memory will be gone.

Sometime in the later, you will eventually have to free the memory space allocated where thread_result points to.

Upvotes: 0

Related Questions