pthread_exit return value

Posted by Manty on Stack Overflow See other posts from Stack Overflow or by Manty
Published on 2014-08-21T03:31:55Z Indexed on 2014/08/21 4:20 UTC
Read the original article Hit count: 126

This is surprising for me.

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?

© Stack Overflow or respective owner

Related posts about linux

Related posts about multithreading