I\'m writing a console application Windows that creates some objects in the main thread and kicks those off into a loop, only exiting when the user uses the Ctrl-C interrupt. <
You make your loop on a condition variable.
When you receive ctrl-C (SIGINT) you set the condition variable to false and return. The loop will then exit normally.
bool finished = false;
int main()
{
DBClient db;
DataPuller p;
while (!finished)
{
// ... do stuff until Ctrl-C comes in
}
}
// Signal handler or Control handler on windows
// Set finished = true.
I don't think this can be done without at least one global. However, I think you only need one:
BOOL is_shutting_down = false;
BOOL CtrlHandler ( DWORD fdwCtrlType ) {
switch( fdwCtrlType )
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
is_shutting_down = true;
return( TRUE );
}
}
int main() {
DBClient db;
DataPuller p;
while (is_shutting_down == false) {
... do stuff until Ctrl-C comes in
}
//objects clean themselves automatically.
}
A side affect of a global "is_shutting_down" is that if it is set to true, destructors do not need to deallocate memory, since the OS will automatically reclaim it all anyway, which allows you to shut down FAST if you have a large number of small allocations.