当父线程终止时,子线程退出 [英] Do child threads exit when the parent thread terminates

查看:136
本文介绍了当父线程终止时,子线程退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio C ++中使用调用beginthreadex,endthreadex来执行一些多线程编程。

I was doing some multithreaded programming in Visual studio C++ using the calls beginthreadex, endthreadex.

我创建一个子线程thread1。子线程运行在一个从不退出的函数上,因为它有一个无限循环。现在如果父线程以错误终止或成功完成,子线程是否也退出?我的怀疑是 - 有没有任何情况下子线程仍然活着,即使在主程序退出后?

I create a child thread thread1. The child thread runs on a function which never exits as it has an infinite loop. Now if the parent thread terminates with error or finishes successfully, does the child thread also exit? My doubt is - is there any situation where the child thread is alive even after the main program exits?

对于linux,这种情况如何?

For linux how should this case be?

推荐答案

线程之间没有父/子关系。如果线程A创建线程B,然后线程A终止,则线程B将继续执行。

There is no parent/child relationship between threads. If thread A creates thread B and then thread A terminates, then thread B will continue to execute.

例外情况是当主线程(即线程运行 main()函数)终止。发生这种情况时,进程终止,所有其他线程停止。

The exception to this is when the main thread (that is, the thread that runs the main() function) terminates. When this happens, the process terminates and all other threads stop.

这篇关于当父线程终止时,子线程退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆