Click to See Complete Forum and Search --> : basic thread question


potatoCode
March 30th, 2009, 07:14 PM
Hi

I have a question about when thread terminates.
Say I have the following

void foo()
{
Thread(/* all the params */); // e.g. CreateThread()
}

int main()
{
foo();
}
My questions are;

Q1. If I dont explicitly call ExitThread(), when does the process created by Thread() terminates?

Q2. Do all the threads created inside main() end when the program exits
without having called the functions that end the threads?

Thanks alot for the help~

Syoleen
April 1st, 2009, 02:58 AM
Answer to Q1: All Thread must has a method run(). When the method run() finished, the thread ended.

Answer to Q2: Other threads will not be terminated automatically when the main() method finishes. But I think all threads will be terminated when you call System.exit().

Codeplug
April 1st, 2009, 11:22 AM
Q1: Windows/Posix - Consider the thread "no longer running" when you return from the thread function.

Q2: Windows(MSCRT)/Posix - Returning from main() is the same as calling exit() - which kills all threads and the process.

gg

potatoCode
April 2nd, 2009, 06:29 AM
thank you Syoleen and Codeplug!

That makes it more clear.