Handle signals properly on Windows#1123
Conversation
|
This is well-intentioned, but it seems we have fiddled a lot with the signal handler and the console state, just to get something that isn't even very user-friendly, see #1158. As there doesn't seem to be a simple, portable way of doing non-blocking console input, maybe a solution could be to start a thread that simply uses blocking input functions. We could then let Ctrl-C end the program without catching the signal. A disadvantage would be that colors wouldn't be restored. That's not a vote against this PR, just throwing out this thought... |
|
I just figured if we were doing it, we should do it right. I also agree it's a bit messy to use Personally, I'd be happy to submit a patch that removes As for doing this on another thread, I even mentioned handling input on another thread in #1040 (which has some console changes) under
Since it processes one key at a time to allow us to clean up control characters such as |
21dce58 to
1001438
Compare
1001438 to
db06041
Compare
This uses Window's
SetConsoleCtrlHandlerto handle ctrl+c.We currently use
signalbut Windows' official documentation forsignalwarns us:This wasn't a big deal before but since we now print timings on ctrl+c exit (1021), we should probably handle the signal properly.
The documentation for
SetConsoleCtrlHandlerhas no such warning and their example code even shows them usingprintf.Another advantage to doing it this way is we don't have to reinitialize SIGINT every loop in main like we currently do.
The only downside I see to this is the inclusion of <windows.h>, but fortunately we can cut down on what the header pulls in by defining
WIN32_LEAN_AND_MEAN. If this still isn't satisfactory, I can change this to do a little cluster of defines at the top as is done in common.cpp.