당신은 ncurses 사용하거나 당신이하지 않으려면이 blog post에 설명 된대로, 당신은 선택 사용할 수 있습니다. 기본적으로 select
을 사용하고 제한 시간을 지정할 수 있습니다. stdin FD가 설정되면 안전하게 읽을 수 있으며 차단되지 않습니다. 선택에 대한 자세한 정보가 필요하면 this out과 물론 Wikipedia을 확인하십시오. 그것은 알기에 편리한 전화입니다. 예를 들어,
// if != 0, then there is data to be read on stdin
int kbhit()
{
// timeout structure passed into select
struct timeval tv;
// fd_set passed into select
fd_set fds;
// Set up the timeout. here we can wait for 1 second
tv.tv_sec = 1;
tv.tv_usec = 0;
// Zero out the fd_set - make sure it's pristine
FD_ZERO(&fds);
// Set the FD that we want to read
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
// select takes the last file descriptor value + 1 in the fdset to check,
// the fdset for reads, writes, and errors. We are only passing in reads.
// the last parameter is the timeout. select will return if an FD is ready or
// the timeout has occurred
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
// return 0 if STDIN is not ready to be read.
return FD_ISSET(STDIN_FILENO, &fds);
}
는 스레드가 이것에 대한 과잉이다
Peek stdin using pthreads
는 새로운 스레드에서 입력을 읽은 다음 주 스레드에서 스레드를 죽일 수 십초이 경과 한 후에? – GWW
@ GWW의 제안 외에 항상 낮은 수준의 OS 기능을 사용할 수 있습니다. Linux/* nix에서는 "원시"I/O라고 불렀습니다. 그러나 그 일을한지 오래되었습니다. –