Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top