c++ - Pthreads,与 pthread_join(pthread_t, void**) 混淆

标签 c++ c pthreads pthread-join

我不明白为什么 pthread_joinvoid** 作为第二个参数作为返回值,而 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/

相关文章:

c - 多键一值数据结构 (C)

boost::smart_ptr 的 C++ 非侵入式 boost 序列化

c++ - C++中虚方法的误解

C 中的 SQL 解析器

c++ - 在 libpthread 链接应用程序中捕获异常时出现段错误(linux、C++)

c++ - 程序终止时清理 pthread 资源

c - 当线程在已解锁的互斥锁上调用 pthread_mutex_unlock 时会发生什么

c++ - 从指针 vector 中删除元素

c++ - 指针作为模板参数?

c - 是 snprintf(NULL,0,...);行为规范?