#include <pthread.h> #include <unistd.h> static void *tfunc(void *data) { return NULL; } int main(int argc, char **argv) { pthread_t t; pthread_create(&t, NULL, tfunc, NULL); sleep(1); pthread_detach(t); return 0; }
参见MWE.
它工作正常,但我不确定这是否是实际定义的行为. pthread_detach的手册页没有说明在退出的线程上调用它.
是的我知道创建具有分离属性的线程,但我对这种情况特别好奇. pthread_join在这个案例中有提及,我认为pthread_detach的工作原理一样好,但我没有找到任何官方声明.
此代码完全合法,并且不会调用未定义的行为:#include <pthread.h> #include <unistd.h> static void *tfunc(void *data) { return NULL; } int main(int argc, char **argv) { pthread_t t; pthread_create(&t, NULL, tfunc, NULL); sleep(1); pthread_detach(t); return 0; }
它没有明确说明,但是POSIX documentation for pthread_detach()
的措辞是这样的,它必须被定义并且在终止线程上调用pthread_detach()是正确的:
The
pthread_detach()
function shall indicate to the implementation
that storage for the thread thread can be reclaimed when that thread
terminates. If thread has not terminated,pthread_detach()
shall not
cause it to terminate.The behavior is undefined if the value specified by the thread
argument topthread_detach()
does not refer to a joinable thread.
首先,请注意语句“如果线程尚未终止”.这意味着当线程终止时调用pthread_detach()必须是安全的.
其次,请注意“如果……不引用可连接线程,则行为未定义”.在您发布的代码中,您创建的线程显然是可连接的 – 您没有使用分离属性创建它,因此您可以调用pthread_join()来检索其返回值.所以这不是未定义的行为.
请记住,当调用pthread_join()或pthread_detach()时,无法保证从线程A确保线程B仍在运行.因此,要么从任何其他线程上的任何线程调用(一次!),要么是安全的.
另外,从POSIX文档的Rationale部分:
RATIONALE
The
pthread_join()
orpthread_detach()
functions should eventually
be called for every thread that is created so that storage associated
with the thread may be reclaimed.It has been suggested that a “detach” function is not necessary; the
detachstate thread creation attribute is sufficient, since a thread
need never be dynamically detached. However, need arises in at least
two cases:
In a cancellation handler for a
pthread_join()
it is nearly essential to have apthread_detach()
function in order to detach the
thread on whichpthread_join()
was waiting. Without it, it would be
necessary to have the handler do anotherpthread_join()
to attempt
to detach the thread, which would both delay the cancellation
processing for an unbounded period and introduce a new call to
pthread_join()
, which might itself need a cancellation handler. A
dynamic detach is nearly essential in this case.In order to detach the “initial thread” (as may be desirable in processes that set up server threads).
同样,虽然没有明确说明,但请注意pthread_join()和pthread_detach()之间的隐含等价.