简体   繁体   English

分离线程然后使其超出范围(并使其仍在运行)是否安全?

[英]is it safe to detach a thread and then let it go out of scope (and have it still running)?

I have the following code, which I think works ok (forgive the silly/contrived example). 我有以下代码,我认为可以正常工作(原谅愚蠢/人为的示例)。

void run_thread()
{
    std::thread t([]{
        while(true)
        {
            // keep getting chars... to stop peoples eye's hurting : )
            char c = getchar();
        }
    });

    t.detach(); // Detach thread

    // thread goes out of scope here - but is it ok because its detached??
}

int main()
{
     run_thread();    

    // Wait here forever
    while (true) {;}
}

But after re-reading it I have a doubt about it. 但是在重新阅读之后,我对此表示怀疑。 Thread t goes out of scope. 线程t超出范围。 I can't remember now if it is safe to do this after you have called detach()... I think it is, but as I say I have a nagging doubt. 我现在不记得在调用detach()之后这样做是否安全了……我认为是,但是正如我所说的那样,我有一个令人困扰的疑问。 Can anyone confirm if this is good/bad practise? 谁能确认这是好事还是坏事?

Thread t goes out of scope. 线程t超出范围。 I can't remember now if it is safe to do this after you have called detach() 我现在不记得在调用detach()之后这样做是否安全

You detach() because you want to disassociate the actual running thread with the thread object. 之所以使用detach()是因为您要取消实际正在运行的线程与线程对象的关联。 So after } t goes out of scope but the actual thread will keep on running until its instruction completes. 因此,在} t超出范围后,实际线程将继续运行,直到其指令完成。

If it weren't for detach() std::terminate would have killed the thread at } 如果不是detach() std::terminate将会杀死}的线程

detach basically releases the std::thread object instance which is the C++ "handle" to the actual OS thread, thereby making it impossible to join the thread later. detach基本上将std::thread对象实例释放为实际OS线程的C ++“句柄”,从而使以后无法join该线程。

In most cases it's better to keep the thread instance around at some global scope so that you can join it later, for example before exiting main . 在大多数情况下,最好将thread实例保留在某个全局范围内,以便以后可以join它,例如在退出main之前。 That way you can ensure all threads finish before the main thread. 这样,您可以确保所有线程在主线程之前完成。

For example: 例如:

std::thread t; // can be "empty"

void run_thread()
{
    t = std::thread([]{
        while(true)
        {
            // keep getting chars...
            char c = getchar();
        }
    });

}

int main()
{
     run_thread();    

    // Wait here
    std::this_thread::sleep_for(30s);

    // Before exiting wait for the thread to finish
    if (t.joinable())
        t.join();
}

这种用法是分离的地步

Yes, it is ok and safe in you code. 是的,在您的代码中可以安全进行。 But it does not have any sense. 但这没有任何意义。 main function will utilize CPU and a thread function will get less CPU time. main功能将利用CPU,而线程功能将减少CPU时间。 You can attach to forever thread and reach similar behaviour: run_thread will never exit, thus main will never exit. 您可以附加到永久线程并达到类似的行为: run_thread将永远不会退出,因此main永远不会退出。

void run_thread()
{
    std::thread t([]{
        while(true){/* also run forever */;}
    });

    // Wait here forever
    t.attach();
}

int main()
{
     run_thread();    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用异步触发线程并超越其未来范围是否安全? - Is it safe to shoot off a thread using async and go out the scope of its future? 如果分离的std :: thread使用超出范围的对象,是否安全? - Is it safe if detached std::thread uses object that went out of scope? 当std :: lock_guard仍然在作用域内时,使用pthread_create创建线程是否安全? - Is it safe to create a thread using pthread_create when std::lock_guard is still in scope? std :: thread不是全局变量,但是到达创建它的函数的末尾时不会超出范围吗? - std::thread that isn't a global variable but doesn't go out of scope when the end of the function that created it is reached? 提升线程 - 超出范围的可能性 - Boost thread - Out of scope possibility 是否始终安全地创建一个线程并让“this”指针从构造函数中逃脱? - Is it always safe to create a thread and let “this” pointer escape from a constructor? c ++:在线程仍在运行时,可以给它赋予新值吗?还是必须先结束它? - c++: can I give a new value to a thread, while it is still running or do I have to end it first? 有一个安全的方法让std :: thread作为类的成员吗? - Is there a safe way to have a std::thread as a member of a class? 线程::加入与分离 - Thread::Join vs detach CPP,变量不会超出范围 - CPP, Variables don't go out of scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM