I check 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):
according to the pthread_join man page, we should get an “ EINVAL ” error when we try to join a non-joinable thread, however, none of the above cases got an errno set. And also in the first case, it seemed that we can even get the exit status of a separate stream, I'm confused by the result. Can anyone explain this? Thanks
[EDIT]: I realized that the first printf can reset 'errno', however, even after I changed the order of the two printf statements, I still have the same results.
source share