高分辨率计划时间

3 c++ precision time

我目前正在用C++编写俄罗斯方块.现在我正处于编写程序的阶段,但我仍然需要修复一些错误并优化性能.

话虽这么说,我的程序中的一个缺点是它每秒只能处理一次按键操作.我需要它至少处理三个.您可以看到此代码演示的缺陷:

//Most headers only pertain to my main program.
#include <iostream>
#include <termios.h>
#include <pthread.h>
#include <time.h>
#include <cstring>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

using namespace std;

//Timer function.
void *Timer(void*) {

    time_t time1, time2;

    time1 = time(NULL);

    while (time2 - time1 < 1) {
        time2 = time(NULL);
    }

    pthread_exit(NULL);
}

int main() {

    //Remove canonical buffering.
    struct termios t_old, t_new;
    tcgetattr(STDIN_FILENO, &t_old);
    t_new = t_old;
    t_new.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &t_new);

    const int STDIN = 0;

    struct timeval tv, tv1;
    fd_set readfds, readfds2, master;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    FD_ZERO(&readfds);
    FD_ZERO(&master);    
    FD_SET(STDIN, &readfds);
    FD_SET(STDIN, &master);
    char buffer[1];

    while(buffer[0] != 'q') {

        pthread_t inputTimer;

        pthread_create(&inputTimer, NULL, Timer, NULL);

        readfds = master;

        memcpy(&tv1, &tv, sizeof(tv));

        if (select(STDIN+1, &readfds, NULL, NULL, &tv1) == -1) {
            perror("select");
        }
        if (FD_ISSET(STDIN, &readfds)) {
            buffer[0] = cin.get();
            cout << "You entered: " << buffer << endl;

        }

        pthread_join(inputTimer, NULL);


        cout << "Timed out.\n" << endl;

    }

    cout << "Game Over." << endl;

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

如您所见,程序通过设置一秒间隔计时器和timeval来运行.因为两个计时器都使用整数来确定已经过了多少时间,所以它们不能比一秒更精确.如何更精确地修改程序?

我的想法是,tv1如果按下一个键,则将值复制到第三个值,然后再次等待输入,但不管tv1是什么时间值.例如,如果我只剩下半秒钟时按一个键,则该值0.5将从tv1另一个变量中获取并复制到另一个变量.然后程序只等待半秒钟输入,而不是整秒.然而,这不起作用,因为tv1只有等于10.

Cla*_*dix 5

尝试使用struct timevalgettimeofday() in sys/time.h.您可以实现微秒分辨率.

Manpage:http://linux.die.net/man/3/gettimeofday

更多信息:http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html

编辑:在Linux(不能移植到Windows下的MinGW)你也可以使用poll()(见这里),它可以让你等待毫秒.这会更有效,因为poll暂停线程执行直到时间结束.

//Timer function.
void *Timer(void*) {
    poll(0, 0, 100); //Suspend thread for 100 ms. 
    pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

poll函数在中声明poll.h