Do child threads exit when the parent thread terminates

后端 未结 3 1532
-上瘾入骨i
-上瘾入骨i 2020-12-01 01:36

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

I create a child thread thread1. The child thread runs on

相关标签:
3条回答
  • 2020-12-01 01:57

    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.

    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.

    0 讨论(0)
  • 2020-12-01 02:04

    As soon as your process die, all the resources are being released (memory, files and threads)

    The correct way to do this: when you call beginthread, keep the returned handle in the parent thread, and call WaitForObject before you leave the program (we join the parent thread with the child thread).

    The parent thread will block until the child thread finish. If your child thread has a infinite loop, you could define an "interruption point" , and check if you should leave. For example, using a shared boolean variable. Check Interrupt Politely fro more info.

    0 讨论(0)
  • 2020-12-01 02:20

    Since C and C++ mandate that returning from the main function kills all running threads, yes, the process should be gone. And since that behavior is done by the runtime the situation should be the same on Linux.

    0 讨论(0)
提交回复
热议问题