chu*_*333 3 c pthreads pthread-join
我正在检查'pthread_join'的行为并具有以下代码:
#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);
}
Run Code Online (Sandbox Code Playgroud)
在我的平台上观察到的输出(Linux 3.2.0-32-generic#51-Ubuntu SMP x86_64 GNU/Linux):
用'sleep(1)'注释掉:42 0
使用sleep语句,它产生:1 0
根据pthread_join的手册页,当我们尝试加入一个不可连接的线程时,我们应该得到错误' EINVAL ',但是,上面的两种情况都没有设置errno.而且在第一种情况下,似乎我们甚至可以获得分离线程的退出状态,我对结果感到困惑.有人能解释一下吗?谢谢
[编辑]:我意识到第一个printf可能会重置'errno',但是,即使我交换了两个'printf'语句的顺序后,我仍然得到了相同的结果.
你的期望是错误的.调用pthread_join分离的线程会调用未定义的行为.无需设置errno,返回错误代码,甚至根本不返回.
如果你需要引用,
如果pthread_join()的thread参数指定的值未引用可连接线程,则行为未定义.
资料来源:http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html
另外,请注意大多数pthread函数,包括pthread_join,不要使用errno.相反,他们将错误代码作为返回值返回.因此,errno调用pthread函数后检查是错误的,即使您在调用它时未调用未定义的行为也是如此.