问题
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.
回答1:
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).
来源:https://stackoverflow.com/questions/14324663/cross-platform-way-of-having-non-blocking-console-input