Boost logo

Boost Users :

Subject: [Boost-users] [asio] deadline_timer not waiting properly?
From: Dylan Klomparens (dylan.klomparens_at_[hidden])
Date: 2011-03-21 18:57:20


Hello,

I tried using the boost deadline_timer in this simple test application, but
had some trouble. The goal is for the timer to trigger every 45 milliseconds
using the "expires_at()" member function of the deadline_timer. (I need an
absolute time, so I'm not considering expires_from_now(). I am also not
concerned about drift at the moment). When I run the program, wait() does
not wait for 45 ms! Yet, no errors are reported. Am I using the library
incorrectly somehow?

-- Dylan

Sample program:

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;

int main()
{
        boost::asio::io_service Service;
        boost::shared_ptr<boost::thread> Thread;
        boost::asio::io_service::work RunForever(Service);
        Thread = boost::shared_ptr<boost::thread>(new
boost::thread(boost::bind(&boost::asio::io_service::run, &Service)));
        boost::shared_ptr<boost::asio::deadline_timer> Timer(new
boost::asio::deadline_timer(Service));

        while(1)
        {
                boost::posix_time::time_duration Duration;
                Duration = boost::posix_time::microseconds(45000);
                boost::posix_time::ptime Start =
boost::posix_time::microsec_clock::local_time();
                boost::posix_time::ptime Deadline = Start + Duration;
                boost::system::error_code Error;
                size_t Result = Timer->expires_at(Deadline, Error);
                cout << Result << ' ' << Error << ' ';
                Timer->wait(Error);
                cout << Error << ' ';
                boost::posix_time::ptime End =
boost::posix_time::microsec_clock::local_time();
                (cout << "Duration = " << (End - Start).total_milliseconds()
<< " milliseconds" << endl).flush();
        }
        return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net