如果存在,等待线程分离本身是什么? [英] What if a being-waited thread detaches itself?
问题描述
的#include< pthreads.h中>无效thread_routine(无效*)
{
睡眠(5);
pthread_detach(pthread_self());
睡眠(5);
}诠释的main()
{
的pthread_t吨;
在pthread_create(& T公司,0,thread_routine,0);
在pthread_join(T);
}
威尔在pthread_join(T);
pthread_detach后立即返回(pthread_self());
成功
该行为是不确定的,因此显然不惜一切代价避免。
(据我可以告诉行为的隐的定义。还有的的明确的不确定行为规范,但没有提到这个确切的情况。)
有关的好奇,一个NPTL Linux系统上靠近我,无论是 pthread_detach()
和在pthread_join()
返回0,并且,此外,后者的块,并成功地获得由线程返回的值。在靠近我的OS X系统,相比之下, pthread_detach()
成功,并在在pthread_join()
立即失败与ESRCH。
#include <pthread.h>
void thread_routine(void*)
{
sleep(5);
pthread_detach(pthread_self());
sleep(5);
}
int main()
{
pthread_t t;
pthread_create(&t, 0, thread_routine, 0);
pthread_join(t);
}
Will pthread_join(t);
return immediately after pthread_detach(pthread_self());
succeed?
The behavior is undefined, and thus obviously to be avoided at all costs.
(As far as I can tell the behavior is implicitly undefined. There are several kindred instances of explicitly undefined behavior in the spec, but this exact scenario is not mentioned.)
For the curious, on an NPTL Linux system near me, both the pthread_detach()
and the pthread_join()
return 0, and, moreover, the latter blocks and successfully gets the value returned by the thread. On an OS X system near me, by contrast, the pthread_detach()
succeeds, and the pthread_join()
immediately fails with ESRCH.
这篇关于如果存在,等待线程分离本身是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!