非阻塞控制台输入 C++
我正在寻找一种(多平台)方式来为我的 C++ 程序进行非阻塞控制台输入,这样我就可以在程序持续运行的同时处理用户命令.程序也会同时输出信息.
I'm looking for a (multiplatform) way to do non-blocking console input for my C++ program, so I can handle user commands while the program continually runs. The program will also be outputting information at the same time.
最好/最简单的方法是什么?我在使用像 boost 这样的外部库时没有问题,只要它们使用宽松的许可证即可.
What's the best/easiest way to do this? I have no problem using external libraries like boost, as long as they use a permissive license.
推荐答案
我会通过创建一个单独的线程来实现这一点,该线程调用普通的阻塞 IO 函数并传递给它一个回调函数,当它收到输入时会调用该函数.你确定你需要做你说你想做的事吗?
I would do this by creating separate a thread which calls normal blocking IO functions and pass it a callback function which it would call when it got input. Are you sure you need to do what you said you want to do?
对于同时输出信息,如果用户正在输入一些输入并且你打印了一些东西会发生什么?
As for outputting information at the same time, what would happen if the user was in the middle of typing some input and you printed something?
相关文章