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 witho开发者_开发知识库ut being allocated any space?
//Update-1: Add the definition of pthread_exit.
//Update-2: Add the definition of thread_function.
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.
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.
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.
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.
What user482594 said
Also:
The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated. On return from a successful pthread_join() call with a non- NULL value_ptr argument, the value passed to pthread_exit() by the terminating thread shall be made available in the location referenced by value_ptr. When a pthread_join() returns successfully, the target thread has been terminated. The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined. If the thread calling pthread_join() is canceled, then the target thread shall not be detached.
精彩评论