非阻塞控制台输入 C++

Non-blocking console input C++(非阻塞控制台输入 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++

基础教程推荐