Cross-platform way of having non-blocking console input

a 夏天 提交于 2019-12-23 02:08:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!