且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户输入而无需暂停代码(C ++控制台应用程序)

更新时间:2022-02-08 23:02:17

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

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.