I'm currently writing something of a quiz program. This program throws questions at the user until a specified time limit runs out. How it's set up now is the quizzing functionality runs in a boost thread and the timing aspect is handled by a timed_join() on that thread. The problem I'm encountering is when the user is answering a question using cin, the thread will go past the time limit that has been established. Is there a way to have it interrupt the cin call?
Asked
Active
Viewed 466 times
3 Answers
2
You can read the input character by character in a non-blocking read by using getchar
, getch
or getche
. If you've been looping long enough to reach the timeout, then stop looping :).

Kiril
- 39,672
- 31
- 167
- 226
-
+1 for simplicity. For small project, it is how I would do it. – Joe McGrath Nov 07 '11 at 19:26
2
You can use Boost.Asio to read from cin
asynchronously as described here - updated link to example code is here.

Community
- 1
- 1

Steve Townsend
- 53,498
- 9
- 91
- 140
0
You might need to use other input methods. The readline library might be able to help you. Or if you are on Linux, you can go down to pure file descriptors, make STDIN_FILENO
non-blocking like a non-blocking socket and use the select
system-call and then you can get both the timeout and know when input is ready.

Some programmer dude
- 400,186
- 35
- 402
- 621