非阻塞控制台输入C ++ [英] Non-blocking console input C++

查看:769
本文介绍了非阻塞控制台输入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,只要他们使用permissive许可证。

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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆