pthread: join a detached thread doesn't set errno correctly

邮差的信 提交于 2019-12-12 14:26:22

问题


I am checking the behavior of 'pthread_join' and have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

#include <pthread.h>

void *thread(void *vargp)
{
    pthread_detach(pthread_self());
    pthread_exit((void *)42);
}

int main()
{
    int i = 1;
    pthread_t tid;

    pthread_create(&tid, NULL, thread, NULL);

    sleep(1);
    pthread_join(tid, (void **)&i);
    printf("%d\n", i);
    printf("%d\n", errno);
}

Observed output on my platform (Linux 3.2.0-32-generic #51-Ubuntu SMP x86_64 GNU/Linux):

  1. with the 'sleep(1)' commented out: 42 0

  2. with the sleep statement, it produces: 1 0

according to the man page of pthread_join, we should get the error 'EINVAL' when we are trying to join a non-joinable thread, however, neither cases above got the errno set. And also in the first case, it seemed that we can even get the exit status of a detached thread, I am confused with the result. Could anyone explain this? Thanks

[EDIT]: I realized that the first printf may reset 'errno', however, even after I swapped the order of the two 'printf' statements, I still got the same results.


回答1:


Your expectation is wrong. Calling pthread_join on a detached thread invokes undefined behavior. There is no requirement to set errno, return the error code, or even return at all.

In case you need a citation,

The behavior is undefined if the value specified by the thread argument to pthread_join() does not refer to a joinable thread.

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html

Also, note that most pthread functions, including pthread_join, do not use errno. Instead they return the error code as their return value. As such, inspecting errno after calling a pthread function is wrong even if you didn't invoke undefined behavior when you called it.




回答2:


You get this error by reading the return value of function pthread_join, try this:

if (errno = pthread_join(tid,NULL)) {
    printf("An error ocurred: %i", errno);
}


来源:https://stackoverflow.com/questions/13917944/pthread-join-a-detached-thread-doesnt-set-errno-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!