如何在C++中使用计时器在给定时间内强制输入
How to use a timer in C++ to force input within a given time?
我想在C++中实现超时功能。
如果用户在 2 秒内未输入该值,则程序必须显示超时语句并再次询问输入
例如(输出屏幕):
Timer=0;
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input: //if input is not given within 2 seconds then
Time-out: 2 seconds
Timer again set to 0
Please enter the input:22
Data accepted
Terminate the program`
法典:
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t endwait;
endwait = 2000 ;
cout<<endwait;
while (clock() < endwait)
{
cout<<"Please enter the input:";
}
return 0;
}
我已经处理了上面的代码。但这仅在进入 WHILE 循环时发生。我该如何做到这一点,以便获得所需的输出。
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <condition_variable>
using namespace std;
condition_variable cv;
int value;
void read_value() {
cin >> value;
cv.notify_one();
}
int main()
{
cout << "Please enter the input: ";
thread th(read_value);
mutex mtx;
unique_lock<mutex> lck(mtx);
while (cv.wait_for(lck, chrono::seconds(2)) == cv_status::timeout)
{
cout << "nTime-Out: 2 second:";
cout << "nPlease enter the input:";
}
cout << "You entered: " << value << 'n';
th.join();
return 0;
}
输出:
Please enter the input:
Time-Out: 2 second:
Please enter the input:
Time-Out: 2 second:
Please enter the input:22
You entered: 22
我认为没有必要
为此目的使用复杂的代码(多线程或互斥锁)。请参阅下面的代码:
#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;
int main()
{
int numInput;
clock_t start = clock();
cout << "Timer: 2 sec"<<endl;
cout << "Please enter the input: ";
while ( ! _kbhit() ) //Check for keyboard hit
{
//Check if 2 sec timer expired or not
if (((clock () - start)/ CLOCKS_PER_SEC ) >= 2)
{
cout << "nTimeout 2 sec . . ." << endl;
cout << "Please enter the input: ";
start = clock(); //Set new timer
}
}
//Get the input here
cin >> numInput;
cout << "Data accepted: " << numInput << endl;
_getch();
return 0;
}
恐
怕以标准方式是不可能的,因为默认情况下,I/O 操作会阻止调用进程,直到它最终确定或遇到错误。
您可以通过创建一个线程来检查输入是否已完成并在需要时进入睡眠状态。但这并不实际。
问题在于iostream
/FILE
给你的抽象:你无法访问底层源代码,操作系统也"理解"源代码,因此能够为您提供这种功能(即I/O轮询)。
单独的线程是不够的,因为在发生超时后控制台读取函数仍在运行。
在 POSIX 上,您可以设置一个计时器,该计时器产生信号并导致读取失败并出现-EINTR
。
在 Windows 上,您可以使用 ReadConsoleInput
和 WaitForSingleObject
...但是你需要做自己的行缓冲。
另一个想法是在OVERLAPPED
模式下使用 ReadFile
并在超时的情况下等待完成事件,但这不适用于控制台,请参阅使用重叠 IO 进行控制台输入?
最后,新的Windows版本(Vista及更高版本)允许您使用CancelIoEx
克服"阻止读取未因超时而取消"问题。 如果从单独的线程调用它,它将触发ReadFile
提前返回;您不必自己实现行缓冲。
我使用 kbhit() 函数来解决您的问题。代码如下:-
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int i;
DWORD start_time, check_time;
start_time=GetTickCount();
check_time=start_time+2000;
while(!kbhit())
{
cout<<"Please enter the input:"<<endl;
while((check_time>GetTickCount()))
{
if (kbhit())
{
i=getch();
cout<<"Data accepted"<<endl;
return 0;
}
}
cout<<"Time-out: 2 seconds"<<endl;
cout<<"Timer again set to 0"<<endl;
start_time=GetTickCount();
check_time=start_time+2000;
}
return 0;
}
相关文章:
- boost::进程间消息队列引发错误
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- Ardunio UNO解决了多个重叠的定时器循环
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- 寻找一种单一的定时方法来测试各种算法,不包括它们的输入
- 无法定时输入:C++
- 使用重定向输入和输出的c++进程间通信不适用于发布版本
- 如何验证短时间格式的输入字符串