我不明白为什么 pthread_join
将 void**
作为第二个参数作为返回值,而 pthread_exit
是给定的返回值,返回值参数为 void*
。
最佳答案
pthread_join 等待线程结束,并将 pthread_exit 的结果值存储到 *value_ptr 中。如果你想忽略结果,你可以为 value_ptr 传递 NULL。这是通过将指针传递给变量来模拟按引用传递的常见 C 实践。参见 Passing by reference in C
pthread_join成功时返回0作为函数返回值;然后你知道线程已经加入,你可以访问 *value_ptr 的值。
void *value = NULL;
if (pthread_join(thread, &value) == 0) {
// thread has ended, and the exit value is available in
// the value variable
}
关于c++ - Pthreads,与 pthread_join(pthread_t, void**) 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872425/