Boost.Asio:具有0毫秒到期的deadline_timer的行为是什么

时间:2012-03-12 23:54:45

标签: boost timer boost-asio

到期时间为0毫秒的deadline_timer的行为是什么?

在我的代码中,我有:

boost::asio::io_service ios;
...
boost::asio::deadline_timer ptimer(ios);
ptimer.expires_from_now(boost::posix_time::milliseconds(duration)); // Duration might be 0 sometimes
boost::system::error_code ec;
ptimer.async_wait(boost::bind(&SomeTimeOutHandler, this, ec));

我发现如果duration == 0,处理程序SomeTimeOutHandler永远不会被调用。

我希望它被调用。

但是,如果我更改为duration == 1,则会调用处理程序。

那么deadline_timer到期时的确切行为是什么?

修改

但是下面的HelloWorld测试程序正在运行(由@Roger建议):

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

void SomeTimeOutHandler(const boost::system::error_code& ec)
{
    if (ec)
    {
        std::cout << "SomeTimeOutHandler error_code" << std::endl;
    }
    else
    {
        std::cout << "I'm in good shape" << std::endl;
    }
}

void Test(int duration)
{
    boost::asio::io_service ios;
    boost::asio::deadline_timer ptimer(ios);

    ptimer.expires_from_now(boost::posix_time::milliseconds(duration));
    boost::system::error_code ec;

    ptimer.async_wait(boost::bind(&SomeTimeOutHandler, ec));

    ios.run();

//    boost::this_thread::sleep(boost::posix_time::milliseconds(duration * 2 + 1000));
}


int main(int argc, char* argv[])
{
    Test(10); // Test(0);

    return 0;
}

0 个答案:

没有答案
相关问题