[asio] deadline_timer not waiting properly?
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; }
That worked. Thanks Igor! On Mon, Mar 21, 2011 at 7:34 PM, Igor R <boost.lists@gmail.com> wrote:
boost::posix_time::ptime Start = boost::posix_time::microsec_clock::local_time();
change to universal_time()
boost::posix_time::microsec_clock::local_time();
change to universal_time() _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Dylan Klomparens
-
Igor R