pthread_detach问题

发布于 2024-11-08 04:13:43 字数 745 浏览 0 评论 0原文

直到最近,我的印象是,如果您在生成线程后“分离”它,那么即使“主”线程终止后,该线程仍然存在。

但一个小实验(如下所列)与我的信念相反。我希望分离的线程即使在 main 终止后也能继续打印“从分离的线程说话”,但这似乎没有发生。应用程序显然终止了...

“主”问题返回0之后“分离”线程是否会死亡?

#include <pthread.h>
#include <stdio.h>

void *func(void *data)
{
    while (1)
    {
        printf("Speaking from the detached thread...\n");
        sleep(5);
    }
    pthread_exit(NULL);
}

int main()
{
    pthread_t handle;
    if (!pthread_create(&handle, NULL, func, NULL))
    {
        printf("Thread create successfully !!!\n");
        if ( ! pthread_detach(handle) )
            printf("Thread detached successfully !!!\n");
    }

    sleep(5);
    printf("Main thread dying...\n");
    return 0;
}

Till recently, I was under the impression that if you "detach" a thread after spawning it, the thread lives even after the "main" thread terminates.

But a little experiment (listed below) goes contrary to my belief. I expected the detached thread to keep printing "Speaking from the detached thread" even after main terminated, but this does not seem to be happening. The application apparently terminates...

Do the "detached" threads die after "main" issues return 0?

#include <pthread.h>
#include <stdio.h>

void *func(void *data)
{
    while (1)
    {
        printf("Speaking from the detached thread...\n");
        sleep(5);
    }
    pthread_exit(NULL);
}

int main()
{
    pthread_t handle;
    if (!pthread_create(&handle, NULL, func, NULL))
    {
        printf("Thread create successfully !!!\n");
        if ( ! pthread_detach(handle) )
            printf("Thread detached successfully !!!\n");
    }

    sleep(5);
    printf("Main thread dying...\n");
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜扑 2024-11-15 04:13:44

pthread_detach 只是意味着您永远不会再次加入线程。这使得 pthread 库知道一旦线程退出(分离的情况),它是否可以立即处理线程资源,或者是否必须保留它们,因为您稍后可能会在线程上调用 pthread_join

一旦 main 返回(或退出),操作系统将捕获所有线程并销毁您的进程。

pthread_detach just means that you are never going to join with the thread again. This allows the pthread library to know whether it can immediately dispose of the thread resources once the thread exits (the detached case) or whether it must keep them around because you may later call pthread_join on the thread.

Once main returns (or exits) the OS will reap all your threads and destroy your process.

浅唱ヾ落雨殇 2024-11-15 04:13:44

pthread_detach 并不做你想象的那样 - 它向实现表明具有指定 ID 的线程正在使用的空间一旦终止就可以回收,即。不会对其执行任何pthread_join操作。

一旦包含线程的进程终止,所有线程也将终止。

pthread_detach does not do what you think it does - it indicates to the implementation that the space the thread with the specified ID is using can be reclaimed as soon as it terminates, ie. no pthread_join operation will be performed on it.

All threads are terminated once the process containing them is terminated.

凉世弥音 2024-11-15 04:13:44

是的,分离的线程将在return 0后死亡。

来自man pthread_detach的NOTES部分

分离属性仅仅是
决定系统的行为
当线程终止时;确实如此
不阻止线程
如果进程终止则终止
使用 exit(3) (或者等效地,如果
主线程返回)

Yes, the detached threads will die after return 0.

From the NOTES section of man pthread_detach

The detached attribute merely
determines the behavior of the system
when the thread terminates; it does
not prevent the thread from being
terminated if the process terminates
using exit(3) (or equiv‐alently, if
the main thread returns)

浅浅 2024-11-15 04:13:44

来自man pthread_detach:

pthread_detach() 函数将 thread 标识的线程标记为已分离。当分离线程终止时,其资源会自动释放回系统,而不需要另一个线程加入终止的线程。

From man pthread_detach:

The pthread_detach() function marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.

呢古 2024-11-15 04:13:43

引用Linux 程序员手册

分离属性仅仅是
决定系统的行为
当线程终止时;确实如此
不阻止线程
如果进程终止则终止
使用 exit(3) (或者等效地,如果
主线程返回)。

另来自 Linux 程序员手册

允许其他线程继续
执行时,主线程应该
通过调用 pthread_exit() 终止
而不是 exit(3)

To quote the Linux Programmer's Manual:

The detached attribute merely
determines the behavior of the system
when the thread terminates; it does
not prevent the thread from being
terminated if the process terminates
using exit(3) (or equivalently, if the
main thread returns).

Also from the Linux Programmer's Manual:

To allow other threads to continue
execution, the main thread should
terminate by calling pthread_exit()
rather than exit(3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文