Of Threads and Things

Started by
5 comments, last by Psepha 24 years, 5 months ago
Why not just use the WM_CLOSE message, and delete your stuff there?

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

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
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
}
}

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

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
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

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

Thanks people - Alastair - Looks like the solution I'm gonna try - Seen it before but never used it

Thanks All

Psepha

This topic is closed to new replies.

Advertisement