[英]pthread_exit return value
這讓我感到驚訝。
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
的地址。
問 :您還返回什么?
答 :`ret的地址。
問 :那么調用者取消引用后會得到什么?
答 : ret
的當前值。
您可能想要pthread_exit(ret);
-返回ret
的值 ,以及調用pthread_join
的代碼中的相應更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.