Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypassing blocking input stream with C++/Boost

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?

like image 487
Will Klingelsmith Avatar asked Oct 23 '25 17:10

Will Klingelsmith


2 Answers

You can use Boost.Asio to read from cin asynchronously as described here - updated link to example code is here.

like image 97
Steve Townsend Avatar answered Oct 26 '25 05:10

Steve Townsend


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 :).

like image 40
Kiril Avatar answered Oct 26 '25 06:10

Kiril