Non-blocking console input C++(非阻塞控制台输入 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?
这篇关于非阻塞控制台输入 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:非阻塞控制台输入 C++
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
