【发布时间】:2011-10-01 09:15:01
【问题描述】:
注意:我已经删除了以下 sn-p 中所有必需的错误检查。
...
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.
*/
问题:pthread_join如何填充thread_result的变量? 由于变量 thread_result 没有分配空间来保存信息, 如果 pthread_join 为 thread_result 分配空间,则主线程必须 释放变量持有的资源。如您所见,代码没有 包括 thread_result 的释放资源。所以我假设 pthread_join 实际上并没有为thread_result分配空间。
现在的新问题是变量 thread_result 如何在不包含信息的情况下包含信息 被分配了任何空间?
//Update-1:添加pthread_exit的定义。
//Update-2:添加thread_function的定义。
【问题讨论】:
标签: c linux multithreading posix ubuntu-10.04