简体   繁体   中英

pthread_exit return value

This is surprising for me.

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;
}

The output is

value of exit status - 50
value of exit status - 20

I was expecting both the times the exit_status would be the actual exit value(50 in my case) of the thread. Instead it is just returning the value of the global variable which I used for pthread_exit. Is it not a bug?

Q : What would you return if you wanted to specify how to get the current value of ret .

A : The address of ret .

Q : What do you return?

A : The address of `ret.

Q : So what does the caller get when they dereference it?

A : The current value of ret .

You probably want pthread_exit(ret); -- returning the value of ret , and corresponding changes in the code that calls pthread_join .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM