用户输入而无需暂停代码(C ++控制台应用程序) [英] User input without pausing code (c++ console application)

查看:214
本文介绍了用户输入而无需暂停代码(C ++控制台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不导致代码停止执行的情况下输入输入?在过去20分钟内,我一直在寻找答案,但没有结果.

How can I enter an input without causing the code to stop executing? I have been searching for an answer during the last 20 minutes without result.

cin >> string;暂停代码AFAIK.

cin >> string; pauses the code AFAIK.

我需要使用多线程,还是有更好的方法? (我什至不知道多线程是否可以工作.)

Would I need to use mulithreading, or is there a better way? (I don't even know if multithreading would work.)

我最近开始学习c ++,至少可以说我是一个初学者,所以请彻底解释并包括我可能需要的任何库,谢谢.

I've recently started learning c++, I am a beginner to say the least, so please explain thoroughly and include any library I might need, thank you.

推荐答案

有两种获取输入而不阻塞(暂停)的算法.第一个是轮询,第二个是事件(中断).

There are two algorithms for getting input without blocking (pausing). The first is polling, the second is by event (interrupt).

轮询涉及定期检查输入.使用键盘,这可能是读取键盘的按键.对于串行端口,这可能意味着检查接收寄存器的状态.

Polling involves periodically checking for input. With a keyboard, this could be reading the keyboard for a keypress. With serial ports, it could mean checking the status of the receive register.

在某些系统上阻塞或等待输入将包括永久轮询,直到收到输入为止.

Blocking or waiting for input on some systems would consist of polling forever, until an input is received.

在某些平台上,检测到输入时将发送事件.例如,Windows OS接收到按下键的事件,并将消息发送给重点任务.在嵌入式系统上,硬件可以在中断向量处取消引用功能指针.

On some platforms, an event is sent when input is detected. For example, Windows OS receives an event that a key was pressed and sends the message to the task in focus. On embedded systems, the hardware could dereference a function pointer at an interrupt vector.

基于事件的系统上的输入阻止意味着进入休眠状态,直到接收到事件为止.

Blocking for input on event based systems means sleeping until the event is received.

标准C ++语言没有提供用于无阻塞检索输入的标准功能. C ++输入函数的实现取决于平台,并且可能会阻塞也可能不会阻塞.例如,平台可以等到收到换行符后再返回单个字符.

The standard C++ language does not provide a standard function for retrieving input without blocking. The implementation of the C++ input functions is platform dependent and may or may not block. For example, the platform could wait until a newline is received before returning a single character.

许多平台或操作系统都具有一些功能,您可以在其中测试端口的输入(轮询),或者在发生输入(事件驱动)时收到通知.由于您未指定要使用的平台,因此详细信息在此处停止.

Many platforms or operating systems have functionality where you can test a port for input (polling) or be notified when the input has occurred (event driven). Since you didn't specify which platform you are using, the details stop here.

这篇关于用户输入而无需暂停代码(C ++控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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