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++
基础教程推荐
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
