Jump to content
  • Advertisement


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


Killing a thread from the outside...

This topic is 5230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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,


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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!