#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 to
pthread_detach()
does not refer to a joinable thread.
首先,注意语句“如果线程尚未终止”。这意味着当线程 终止时调用 pthread_detach()
必须是安全的。
其次,请注意“如果...不引用可连接的线程,则行为未定义。”在您发布的代码中,您创建的线程显然是可连接的——您没有使用分离属性创建它,因此您可以调用 pthread_join()
来检索它的返回值。所以这不是未定义的行为。
请记住,在调用 pthread_join()
或 pthread_detach()
时,无法保证线程 A 线程 B 仍在运行。因此,任何一个调用都必须是安全的,可以从任何其他线程上的任何线程调用(一次!)。
此外,来自 POSIX 文档的基本原理部分:
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 topthread_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()
之间隐含的等价性。
关于为已经退出的线程调用 pthread_detach?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51882300/