Jump to content
  • Advertisement

Archived

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

Psepha

Of Threads and Things

This topic is 6834 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

Why not just use the WM_CLOSE message, and delete your stuff there?

------------------
Dance with me......

Share this post


Link to post
Share on other sites
Advertisement
Umm - The WM_CLOSE message is received on the thread that controls the window - not the background task that controls the sound capture and network send processing. I guess the questio really is how do you close down a thread cleanly when it may need to interact with another thread that may be closing

Share this post


Link to post
Share on other sites
I assume that Function() is running in another thread. If you close down the app while the other thread is in its "Do something" bit then the "delete[] Buffer" will never be reached. This is because when the main process dies, all its threads die immediately, before they can cleanup.

One simple thing you can do set a global flag when you want all your extra threads to terminate. The threads should ocasionally look at this flag and exit their "Do something" bit when it is set.

To stop the main process from terminating before all the threads have cleaned up, you can use the WaitForSingleObject or WaitForMultipleObjects API functions. Just pass them the handle of your thread or threads and they will delay your main process until all the threads have exited.

Something like this:

/*** Main process ***/

bool g_finished = false;

// Called just before main process dies.
void OnQuit()
{
g_finished = true;
WaitForSingleObject(h_thread, INFINITE);
}


/*** Extra thread ***/

void Function()
{
Buffer = new char[100];
DoSomething();
delete[] Buffer;
}

void DoSomething()
{
while (!g_finished)
{
// do stuff
}
}

Share this post


Link to post
Share on other sites
If your window is aware of the thread you are worried about, you can ask the thread if it is finished during the WM_CLOSE message, and wait for it there, which is what I usually do.

-fel

Share this post


Link to post
Share on other sites
Bascially, you combine the above two tactics. In the WM_CLOSE message, a global variable is set when threads are to clean up, then you WaitForMultipleObjects() for your threads to finish up, see the flag and quit, then you quit the main thread. Its not particularly hard or complex.

- Splat

Share this post


Link to post
Share on other sites
Thanks people - Alastair - Looks like the solution I'm gonna try - Seen it before but never used it

Thanks All

Psepha

Share this post


Link to post
Share on other sites
Hi all again.
A windows general coding question this time. Am just completing an network telephone program (game talkover type thing)and all appears to be going to plan (works) except where you close down the application. As you can hit the window close button at any time you can end up with resource leaks.....
eg
Function()
{
Buffer = new char[100];
Do something for a bit...
delete[] Buffer;
}

If you close the application whilst performing the Do something for a bit... code the delete[] function is not called. Hmm...I am assuming this is partly because the sound capture and network transmission side of the app runs on another thread....Hmmm...
Any ideas? Should I create some sort of global flag 'DontKillTheAppYetCosImBusy' type thing? Seems a bit messy.......

Psepha

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!