------------------
Dance with me......
Of Threads and Things
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
}
}
-fel
- Splat
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