If there are child threads of a parent thread running on the windows xp OS, does terminating a parent thread terminate its child thread as well?
There is no such thing as "parent thread" in Windows in terms of recorded information. There isn't even strictly a concept of "parent process" - Windows doesn't record the creator process, only the process from which attributes and handles were inherited. So the answer is no.
EDIT: An example should make it clear: you can create a thread in another process, not just your own. Obviously that thread wouldn't terminate when your thread terminates.
Thanks wj32 and jrbjazz for your answers.
can we terminate the main thread (default thread created with the start of a process) while keeping other threads alive?
No - threads belong to a process, not to other threads. Threads do not terminate if the thread that created them terminates.
Threads terminate when:
- The thread function exits (either normally or through an exception).
- The thread calls ExitThread or another function that ends the thread.
- Any thread calls the TerminateThread function with the id of the thread in question.
- The process ends.
From the MSDN description of the CreateThread() windows API function:
"The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle."
My understanding of how CreateThread works is that you basically request Windows to spawn a thread and assign a pointer to the function that you wish the thread to call. When the process is complete, you "politely" tell windows that you no longer need the thread.
So, I would guess that it all comes down to exactly how the application has been written. Using a helpful development language which provides wrappers for threads will remove a lot of the uncertainty here, particularly if the threading is provided through objects and appropriately garbage collected when the application terminates.
As far as the operating system is concerned, I agree with "wj32" that there really isn't a main thread per se, but I guess the Application Thread itself could be seen as a "main thread" conceptually, provided the application manages thread resource allocation appropriately.