实现一个精确到毫秒的计时器

#include 
#include 
using namespace std;

class TimeTick{
public:
    bool first;
    unsigned long long _delay2msec;
    unsigned long long getCurrentMsec()
    {
        struct timeval tv;
        gettimeofday(&tv,nullptr);
        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
    }
    bool operator ()(unsigned long long msec) {//毫秒
        if(first)
        {
            _delay2msec = getCurrentMsec() + msec;
            first = false;
            return true;
        }

        if(getCurrentMsec() >= _delay2msec)
        {
            _delay2msec =  getCurrentMsec()+ msec;
            return true;
        }
        return false;
    }
};
int main()
{
    TimeTick tt ;
    while(true)
    {
        while(tt(1000ULL))
        {
            cout<<"TimeTick"<

你可能感兴趣的:(实现一个精确到毫秒的计时器)