如何在C++中使用计时器在给定时间内强制输入

How to use a timer in C++ to force input within a given time?

本文关键字:定时间 输入 C++ 计时器      更新时间:2023-10-16

我想在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 上,您可以使用 ReadConsoleInputWaitForSingleObject ...但是你需要做自己的行缓冲。

另一个想法是在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;
    }