Click to See Complete Forum and Search --> : How to exit thread safely?


utiao
May 17th, 2004, 08:59 PM
I have two working thread. When I exit one thread, I want to exit another thread. Now, I can't do that. How do I?
thanks!

Kheun
May 17th, 2004, 09:21 PM
In the simplest way, you can set a flag to indicate to the other thread that it has terminated. As for the other thread, it should be looping and checking the flag. So when it find that the flag is set, it should stop entering the loop and terminate itself .

kuphryn
May 18th, 2004, 12:11 AM
Essentially, one thread will end up waiting for one or more threads to terminate.

Kuphryn

utiao
May 18th, 2004, 02:27 AM
So I can set a flag in every thread. When I want to exit the main thread, I must check the every flag whether they have been set.

VipulPathak
June 3rd, 2004, 10:19 AM
Originally posted by utiao

So I can set a flag in every thread. When I want to exit the main thread, I must check the every flag whether they have been set.

Hi,

You can do this:
1) Create a Global Manual Reset Event (say X), Initially Non Signalled.
2) In every Thread, Inside the Loop, use WaitForSingleObject(X, 50 ms) ;
3) Check if Return Value of WaitForSingleObject() equals WAIT_OBJECT_0, then Cleanup and Exit the Thread.

When You want to Close all the Threads, use SetEvent(X) ;

Regards.

-Vipul Pathak ;