aewarnick
February 26th, 2006, 09:32 AM
I am using _beginthread in my program to multithread. The problem is when the user closes the program, if the threads do not end in 1 second they produce runtime errors when the main thread ends. I have an App::Exit function that takes care of ending all of my programs.
Right now, to solve this problem, I have a kind of hack in place. I have a bool variable in namespace App that is accessable globally called Run. All of my threads will monitor this variable and if it is 0, they will exit, but they only have 1 second to respond and that is the problem. I'll show you what I mean:
void Exit()
{
Run= 0;
if(MainWndHwnd) ShowWindow(MainWndHwnd, SW_HIDE);
Sleep(1000); //1 second for threads to end, otherwise, errors!
if(MainWndHwnd)
{
DestroyWindow(MainWndHwnd); MainWndHwnd= 0; MainWnd= 0;
}
}
I could keep track of all threads that are created and use WaitForMultipleObjects here instead of sleeping but is that the only way? Is there a function that will wait for all threads to terminate without knowing the handles of the threads?
Right now, to solve this problem, I have a kind of hack in place. I have a bool variable in namespace App that is accessable globally called Run. All of my threads will monitor this variable and if it is 0, they will exit, but they only have 1 second to respond and that is the problem. I'll show you what I mean:
void Exit()
{
Run= 0;
if(MainWndHwnd) ShowWindow(MainWndHwnd, SW_HIDE);
Sleep(1000); //1 second for threads to end, otherwise, errors!
if(MainWndHwnd)
{
DestroyWindow(MainWndHwnd); MainWndHwnd= 0; MainWnd= 0;
}
}
I could keep track of all threads that are created and use WaitForMultipleObjects here instead of sleeping but is that the only way? Is there a function that will wait for all threads to terminate without knowing the handles of the threads?