Add timeout for getchar() in POSIX.

Previously, the thread pooling new characters being typed was waiting
for "one more char" being typed before being able to exit.

This patch adds a timeout for getchar(). This way, event if the user do
not press his/her keyboard, the program is still able to exit.

This fixes issue:
https://github.com/GiuseppeCesarano/just-fast/issues/2
This commit is contained in:
ArthurSonzogni 2020-05-01 23:33:21 +02:00
parent 6c45d9e603
commit d8e0504ad0

View File

@ -26,6 +26,7 @@
#else
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#endif
// Quick exit is missing in standard CLang headers
@ -94,10 +95,21 @@ void Win32EventListener(std::atomic<bool>* quit,
// Read char from the terminal.
void UnixEventListener(std::atomic<bool>* quit, Sender<char> sender) {
// TODO(arthursonzogni): Use a timeout so that it doesn't block even if the
// user doesn't generate new chars.
while (!*quit)
sender->Send((char)getchar());
fd_set readfds;
FD_ZERO(&readfds);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 300000;
while (!*quit) {
FD_SET(STDIN_FILENO, &readfds);
if (!select(1, &readfds, NULL, NULL, &timeout))
continue;
char c = getchar();
sender->Send(c);
}
}
#endif