使用boost :: deadlin_timer - timer.expires_at()总是大于microsec_clock :: local_time()

时间:2015-09-14 17:49:27

标签: c++ boost

一个简单的循环可以运行直到计时器到期。 我在线研究了Topic和其他几个问题。 这是我最好的尝试,不幸的是它始终处于过期状态。     #包括     #包括     #包括     #include

using namespace boost::asio;
using namespace boost::posix_time;
using namespace std;

class Timeout
{
private:
    io_service io;
    deadline_timer timer;
public:
    Timeout(int ms) : timer(io)
    {
        auto now = microsec_clock::local_time();
        auto expfromnow = milliseconds(ms);
        timer.expires_from_now(milliseconds(ms));
        io.run();
        auto expire = timer.expires_at();
        cout << "Timeout(int ms)   " << "expire:" << expire << "   now:" << now << "   ms_argument:" << ms << "   expfromnow_argument:" << expfromnow << endl;
    }

    int NotExpired()
    {
        return !Expired();
    }

    int Expired()
    {
        auto expire = timer.expires_at();
        auto now = microsec_clock::local_time();
        int isExpired = (int)(expire < now);
        cout << "Expired()   " << "expire:" << expire << "   now:" << now << "   isExpired:" << isExpired << endl;
        return isExpired;
    }

    ~Timeout()
    {}
};


int main()
{
    int t = 1000;
    cout << "Init " << microsec_clock::local_time() << endl;
    Timeout timeout(t);
    cout << "Loop " << microsec_clock::local_time() << endl;
    while (timeout.NotExpired())
    {
        cout << "Swing " << microsec_clock::local_time() << endl;
        this_thread::sleep_for(chrono::microseconds(t / 2));
    }
    cout << "End " << microsec_clock::local_time() << endl;
    return 0;
}

如何修复超时以使主函数按预期运行?

源现在输出以下内容:

enter image description here

我用红色标出了我认为我机器上的问题。 我在Windows机器和vs2013编译器上工作。 我的提升是boost_1_59_0。

编辑#1: 看到@Tanner Sansbury的评论,我为源代码添加了一些输出,以便隔离问题。

编辑#2:编辑#2: 我盯着输出了一点,我意识到有一个多小时的差异。我现在的时间大概是12岁......我坐在德国......我认为这可能是个问题。

1 个答案:

答案 0 :(得分:0)

解决方案是切换到通用时间。 universal_time不依赖于您当地的时区,expires_from_now似乎也使用了通用时间。如果你考虑全球化,这是一个好主意。

#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <atomic>
#include <thread>

using namespace boost::asio;
using namespace boost::posix_time;
using namespace std;

class Timeout
{
private:
    io_service io;
    deadline_timer timer;
public:
    Timeout(int ms) : timer(io)
    {
        auto now = microsec_clock::universal_time();
        auto expfromnow = milliseconds(ms);
        timer.expires_from_now(milliseconds(ms));
        io.run();
        auto expire = timer.expires_at();
        cout << "Timeout(int ms)   " << "expire:" << expire << "   now:" << now << "   ms_argument:" << ms << "   expfromnow_argument:" << expfromnow << endl;
    }

    int NotExpired()
    {
        return !Expired();
    }

    int Expired()
    {
        auto expire = timer.expires_at();
        auto now = microsec_clock::universal_time();
        int isExpired = (int)(expire < now);
        cout << "Expired()   " << "expire:" << expire << "   now:" << now << "   isExpired:" << isExpired << endl;
        return isExpired;
    }

    ~Timeout()
    {}
};


int main()
{
    int t = 1000;
    cout << "Init " << microsec_clock::universal_time() << endl;
    Timeout timeout(t);
    cout << "Loop " << microsec_clock::universal_time() << endl;
    while (timeout.NotExpired())
    {
        cout << "Swing " << microsec_clock::universal_time() << endl;
        this_thread::sleep_for(chrono::milliseconds(t / 3));
    }
    cout << "End " << microsec_clock::universal_time() << endl;
    return 0;
}

enter image description here