这让我很惊讶。
static int ret = 50;
void * thread_func(void *arg)
{
pthread_exit(&ret);
}
int main(void)
{
pthread_t thr;
int *exit_status;
pthread_create(&thr, NULL, thread_func, NULL);
sleep(2);
pthread_join(thr, (void **)&exit_status);
printf("value of exit status - %d\n", *exit_status);
ret = 20;
pthread_join(thr, (void **)&exit_status);
printf("value of exit status - %d\n", *exit_status);
return 0;
}
输出是
value of exit status - 50
value of exit status - 20
我期望 exit_status 两次都是线程的实际退出值(在我的例子中是 50)。相反,它只是返回我用于 pthread_exit 的全局变量的值。这不是错误吗?
最佳答案
问:如果您想指定如何获取 ret
的当前值,您会返回什么。
A:ret
的地址。
问:你返回了什么?
A:`ret.
的地址问:那么当调用者取消引用它时会得到什么?
A:ret
的当前值。
您可能需要 pthread_exit(ret);
—— 返回 ret
的 value,以及调用 的代码中的相应更改>pthread_join
。
关于linux - pthread_exit 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417820/