I need a crossplatform way of calling std::cin in non blocking way. I know it's possible with threads, but maybe there is a better option? Threds seem to be overkill for this.
Asked
Active
Viewed 1,290 times
1
-
AFAIR [ncurses](http://en.wikipedia.org/wiki/Ncurses) provides such feature. But I'm not sure what's more _'overkill'_, depends on definition. – πάντα ῥεῖ Jan 14 '13 at 19:01
-
I know ncurses, but unfortunatelly I need cross platform solution. – user1873947 Jan 14 '13 at 19:02
-
Have you looked into the readsome function? http://www.cplusplus.com/reference/istream/istream/readsome/ – FrankieTheKneeMan Jan 14 '13 at 19:04
-
There are several platforms supported by ncurses. What platform do you have actually? – πάντα ῥεῖ Jan 14 '13 at 19:04
-
Windows and linux, I think its only for posix. – user1873947 Jan 14 '13 at 19:05
-
Check this out for Windows: http://stackoverflow.com/questions/2713698/ncurses-like-system-for-windows – Fred Larson Jan 14 '13 at 19:07
-
Oh, that's good. And readsome looks good, however does it ignore white characters? – user1873947 Jan 14 '13 at 19:07
-
@FrankieTheKneeMan `readsome` usually only returns the number of characters already in the buffer, and so is useless for this. – James Kanze Jan 14 '13 at 19:11
-
@user1873947 OT: You should address your comments responses (type '@') – πάντα ῥεῖ Jan 14 '13 at 19:15
1 Answers
2
Standard C and C++ do not provide any means to do non blocking I/O. Typically, the C and C++ runtime environment does not see any input at all until the user has pressed the <ENTER>
key.
So, the only options are really to use a separate thread to read the input, or to use platform-specific methods to do the non-blocking input (possibly wrapped so you can easily replace that part when porting to a different platform. Libraries like ncurses
provide these input methods pre-wrapped for a number of platforms).

Bart van Ingen Schenau
- 15,488
- 4
- 32
- 41
-
There are "ncurses" and similar libraries available for several different platforms, which contains code to deal with "get key without blocking" and other similar things. – Mats Petersson Jan 14 '13 at 19:20