簡體   English   中英

pthread_exit返回值

[英]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當前值,將返回什么。

Aret的地址。

:您還返回什么?

:`ret的地址。

:那么調用者取消引用后會得到什么?

ret的當前值。

您可能想要pthread_exit(ret); -返回ret ,以及調用pthread_join的代碼中的相應更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM