跨平台的具有非阻塞控制台输入的方式 [英] Cross-platform way of having non-blocking console input
问题描述
我需要一种以非阻塞方式调用std :: cin的交叉平台方法。我知道这是可能的线程,但也许有一个更好的选择?
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.
推荐答案
标准C和C ++不提供任何方法来执行非阻塞I / O。通常,C和C ++运行时环境在用户按下< ENTER>
键之前根本看不到任何输入。
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.
所以,唯一的选项是真正使用单独的线程来读取输入,或使用平台特定的方法来做非阻塞输入(可能包装,所以你可以很容易地替换该部分时移植到不同的平台。像 ncurses
这样的库提供了为许多平台预先包装的这些输入法)。
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).
这篇关于跨平台的具有非阻塞控制台输入的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!