The specs say:
SIGINT is not supported for any Win32 application. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application, such as one in UNIX, to become multithreaded and cause unexpected behavior.
I think it means by default, the SIG_DFL
handler for SIGINT
will create a new thread, handle the signal then terminate the program. I'd like to know, does Windows support user-defined SIGINT
handler, or will the handler ignored by the OS and the default SIG_DFL
handler will run if a CTRL+C interrupt occurs?
For example, will the following code print Signaled!
?
#include <signal.h>
#include <stdio.h>
volatile sig_atomic_t signaled = 0;
void signal_handler(int) { signaled = 1; }
int main() {
signal(SIGINT, signal_handler);
while (!signaled) {}
printf("Signaled!\n");
}
Actually Win32 does fire SIGINT
on Ctrl-C, with a couple of caveats:
The second point introduces a race condition, and so it might be better to use the Win32 native SetConsoleCtrlHandler
instead.
See also this note in the Windows signal
documentation:
SIGINT
is not supported for any Win32 application. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application, such as one in UNIX, to become multithreaded and cause unexpected behavior.