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

8
我希望在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 循环时。我应该怎样做才能获得所需的输出。

没有标准的干净方式仅在有限时间内接受输入。 - chris
@NathanOliver 你能详细说明一下吗? - Walter
特定平台,等待按键事件。您使用的是哪个平台? - Thomas Matthews
你关心可移植性吗? - edmz
不,根本不是。@black - Ankita Rane
显示剩余2条评论
5个回答

8
#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  

3
虽然看起来有效,但这实际上并没有超时cin。例如,如果用户在提示后输入一个值,但在超时之前没有按下回车键,但在随后的提示中用户及时按下了回车键。用户键入的所有值,无论他们是否按下回车键,都将附加到您的值上,因为流永远不会刷新。您可以考虑停止和重新启动流或在某个时间点刷新输入流。条件变量的使用很好! - Daniel Robertson

2

我认为在这种情况下没有必要使用复杂的代码(多线程或互斥锁)。请参考以下代码:

#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;
}

1
我已经使用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;

    }

1
很抱歉,因为I/O操作会默认阻塞调用进程直到其完成或遇到错误,所以无法以标准方式实现。您可以创建一个线程来检查输入是否已完成并进入睡眠状态,但这并不是真正切实可行的解决方案。
问题在于iostream / FILE提供的抽象化层面:您无法访问底层源数据,也就是操作系统“理解”的一种类型,因此无法获得类似I/O轮询的功能。

1

仅仅开启一个新线程是不够的,因为控制台读取函数在超时发生后仍然在运行。

在 POSIX 上,您可以设置一个计时器来产生一个信号,并导致读取失败并返回 -EINTR

在 Windows 上,您可以使用 ReadConsoleInputWaitForSingleObject 实现低级控制台 I/O 超时...但这样你就需要自己进行行缓冲。

另一个想法是在 OVERLAPPED 模式下使用 ReadFile 并等待完成事件超时,但这对于控制台不起作用,请参阅 Using overlapped IO for console input?

最后,新的 Windows 版本(Vista 及更高版本)允许您通过 CancelIoEx 解决“阻塞读取未被超时取消”的问题。如果您从单独的线程中调用它,它将触发 ReadFile 提前返回;您不必自己实现行缓冲。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接