views:

279

answers:

5

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?

A: 

No, unless parent thread is the main thread of the application.

jrbjazz
No, there is no concept of "main thread".
wj32
On Windows, the concept of main thread or primary thread exists. Check http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx
jrbjazz
To expand on my previous comment: in Windows process lifetime is determined *entirely* by its threads - the process is considered "terminated" when its last thread terminates. The documentation for CreateProcess says "primary thread" because CreateProcess creates both a process *and* an initial thread, because that makes sense. You can in fact create a process with no threads using NtCreateProcess. But I'm going into too much low-level detail here.
wj32
If we have a "default thread" and some other threads spawned by this default thread,what i understood from above comment is that we can terminate the default thread while other threads are alive, did i understood correct?
reonze
Yes, that's what I meant (damn 15 character limit).
wj32
+5  A: 

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.

wj32
+1  A: 

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?

reonze
Yes, read my comments on jrbjazz's answer. You are correct in using the phrase "default thread" - there is *no* "main thread".
wj32
+1  A: 

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.
Joe Gauterin
Great answer, except for the last item. Read my comments on jrbjazz's answer. The process is considered "terminated" when all of its threads exit, not the other way around. And I would also like to make another comment if I may: ExitThread is just a wrapper around TerminateThread. So there is only one way a thread exits, and that's through TerminateThread.
wj32
@wj32 - For the last point, I was thinking of calling 'TerminateProcess' on the process to which the thread belongs.
Joe Gauterin
Ah yes. But in the kernel you can see thatNtTerminateProcess simply terminates the process' threads ;)
wj32
A: 

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.

S.Robins
I would like to add that terminated != thread object freed. A thread object can hang around after it is terminated, and it won't affect process lifetime (although it will make sure the process object hangs around as well).
wj32