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 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?
For linux how should this case be?
Because if parent thread dies or terminates then child thread should also die or terminate. If you run those threads as daemon threads, when main thread finishes, your application will exit.
Yes. "this gets printed before the system. out. println() in the child threads.
Threads are part of the process. If the process exits, they (along with all other process resources) cease to exist.
Thread CreationThe creating thread is the parent thread, and the created thread is a child thread. Note that any thread, including the main program which is run as a thread when it starts, can create child threads at any time.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With