为已经退出的线程调用 pthread_detach?

标签 c pthreads

#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() or pthread_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:

  1. In a cancellation handler for a pthread_join() it is nearly essential to have a pthread_detach() function in order to detach the thread on which pthread_join() was waiting. Without it, it would be necessary to have the handler do another pthread_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.

  2. 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/

相关文章:

c - 为什么我从以下 C 代码中得到段错误?

c - 无法理解为什么我们使用二叉搜索树以及如何使用两个结构

c - 在沉迷于sdl编程之前我应该​​了解多少c编程

c++ - 当取消请求排队时,pthread_cancel() 将如何响应?

c - pthread_cond_wait 无限期地阻塞(休眠)直到 pthread_cond_signal 被 C 中的另一个线程调用?

c - 如何让 MPI 中的所有等级向等级 0 发送一个值,然后阻塞接收所有等级?

c++ - 像 glColorPointer 这样的 OpenGL API 如何知道作为指针传递的数组的大小?

java - 大型数组中元素的并行求和

c - 如何创建 2 个线程并交替打印线程 ID 和从 1 到 20 的数字?

c - C 中 pthread 的竞争条件