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

编程入门 行业动态 更新时间:2024-10-28 12:15:56
本文介绍了用户输入而无需暂停代码(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 ++控制台应用程序)

本文发布于:2023-11-06 17:54:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   代码   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!