简体   繁体   English

如果父/主线程死亡,std :: async调用会发生什么情况

[英]What happens to std::async call if parent/main thread dies

If I am right, the std::async uses a new thread and calls the method in it. 如果我是对的,则std :: async使用新线程并在其中调用方法。 I was wondering what happens if the main thread or the parent thread dies. 我想知道如果主线程或父线程死了会发生什么。 Does the thread controlling the async method dies as well. 控制异步方法的线程是否也会死亡。

There is no concept of a "parent" thread in C++, each thread is independent of the one that it was created by. C ++中没有“父”线程的概念,每个线程都独立于它创建的线程。 However, the main thread is special and if it returns from main() or calls exit() then the entire application is terminated even if other threads are still running. 但是, main线程是特殊的,如果它从main()返回或调用exit()则即使其他线程仍在运行,整个应用程序也会终止。 Once that happens, the program has undefined behaviour if the still-running threads access any global variables or automatic objects that were on the main thread's stack, or use any standard library objects or call any function not permitted in signal handlers. 一旦发生这种情况,如果仍在运行的线程访问主线程堆栈上的任何全局变量或自动对象,或者使用任何标准库对象或调用信号处理程序中不允许的任何函数,则程序将具有未定义的行为。

In short, do not let other threads run after main completes if you expect sensible results. 简而言之,如果您期望合理的结果,请不要让其他线程在main完成后运行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM