Hi all. This is my first question in this community.
I'm working on a multithread C++ server (console program), running on Linux and Windows.
I have a problem with the cleanup code on the win32 side. The linux side is working fine: when I want to shutdown the server, I send SIGINT (with CTRL+C), the signal handler sets a global variable and the main pthread executes the cleanup instructions (joining
other pthreads, freeing heap memory, etc.).
On windows it looks not so simple to get the same behavior. I have written a simple test program to understand how the signal handlers works in windows.
#include <iostream> #include <windows.h> bool running; BOOL WINAPI consoleHandler(DWORD signal) { if (signal == CTRL_C_EVENT) { running = false; std::cout << "[CTRL+C]\n"; return TRUE; } return FALSE; } int main(int argc, char **argv) { running = true; if (!SetConsoleCtrlHandler(consoleHandler, TRUE)) { std::cerr << "Error: " << GetLastError() << '\n'; return -1; } std::cout << "Main thread working hard...\n"; while (running) { ; } for (int i = 0; i < 20; i++) std::cout << "This is the " << i << "th fake cleanup instruction\n"; return 0; }
The output is the following:
$ test.exe Main thread working hard... [CTRL+C] This is the 0th fake cleanup instruction This is the 1th fake cleanup instruction
So the main thread is killed quickly, only after two print-instruction. I have tried to move the "test" cleanup for-loop in the handler function, but is not really helping:
suppose that the handler function looks like this:
BOOL WINAPI consoleHandler(DWORD signal) { if (signal == CTRL_C_EVENT) { running = false; std::cout << "[CTRL+C]\n"; for (int i = 0; i < 20; i++) std::cout << "This is the " << i << "th fake cleanup instruction\n"; return TRUE; } return FALSE; }
Now the behavior is even worse! The output is:
$ test.exe Main thread working hard... [CTRL+C] This is the
Am I missing something obvious? What's the proper way to terminate a console process and executes its cleanup code?