Linux pthread_detach 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6042970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:08:23  来源:igfitidea点击:

pthread_detach question

clinuxmultithreadingpthreads

提问by puffadder

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

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

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

在“主要”问题返回 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;
}

采纳答案by NPE

To quote the Linux Programmer's Manual:

引用Linux 程序员手册

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).

detached 属性仅仅决定了线程终止时系统的行为;如果进程终止使用exit(3)(或等效地,如果主线程返回),它不会阻止线程被终止。

Also from the Linux Programmer's Manual:

同样来自Linux 程序员手册

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

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

回答by idz

pthread_detachjust 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_joinon the thread.

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

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

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

回答by freethinker

Yes, the detached threads will die after return 0.

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

From the NOTES section of man pthread_detach

从注释部分 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)

detached 属性仅仅决定了线程终止时系统的行为;如果进程使用 exit(3) 终止(或等效地,如果主线程返回),它不会阻止线程被终止

回答by Michael Foukarakis

pthread_detachdoes 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_joinoperation will be performed on it.

pthread_detach不做你认为它所做的 - 它向实现表明具有指定 ID 的线程正在使用的空间可以在它终止后立即回收,即。不会pthread_join对其进行任何操作。

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

一旦包含它们的进程终止,所有线程就会终止。

回答by Dinesh

From man pthread_detach:

来自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.

pthread_detach()函数将线程标识的线程标记为分离的。当一个分离的线程终止时,它的资源会自动释放回系统,而无需另一个线程与终止的线程连接。