Archived

This topic is now archived and is closed to further replies.

Goodlife

Killing a thread from the outside...

Recommended Posts

Hi all, I use a thread to load my game graphics. The loading thread is just a linear bunch of statements like: LoadSprite LoadSprite LoadSong LoadSound LoadSprite etc, etc. My problem: If I shut down the game during the load process, the game quits, but the thread keeps on going, until it finally crashes out by trying to access game objects that have already been destroyed. I have been up and down the MS help files, but I can''t find a way to terminate a thread from the outside. Suppose I spawn a thread with beginthread... that returns a thread handle. How do I terminate that thread via the handle? All the thread termination functions seem to demand that they be called from within the thread. Thanks!

Share this post


Link to post
Share on other sites
Well, you can always make a call to TerminateThread(), but I would NOT recommend this approach (read the documentation for this function to see why).

A better approach would be to pass the address of a boolean variable to your thread, say "bKill". If you want to terminate your thread from the outside, set this to TRUE. Inside your thread, check the value of the boolean before each line of your execution. If it is TRUE, then exit the thread.

If each of your loading routines takes a long time, you may want have a periodic check inside your loading routines to see if they should abort...

Hope this helps,

MJB

Share this post


Link to post
Share on other sites
The way i do it is to have a status value. It extends what MJB said.

You have a shared value (int, short, whatever) which is 0
When you want to quit you set the value to 1. Then the main process stops and waits for the value to change to 2.

The loading thread regularly checks the value if it finds a 1 it changes it to 2 and then quits. This way the process waits for the loading thread to shutdown before deleting game objects.

[edited by - empirical on March 21, 2004 10:13:41 AM]

Share this post


Link to post
Share on other sites