Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4101902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:45:25+00:00 2026-05-20T20:45:25+00:00

I tried using the boost deadline_timer in this simple test application, but had some

  • 0

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?

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:45:26+00:00Added an answer on May 20, 2026 at 8:45 pm

    You are mixing local time with system time. The time that asio is comparing your local time to is most likely some number of hours after the time that you want your deadline set to so wait returns immediately (depending on where you live; this same code could wait for several hours as well). To avoid this point of confusion, absolute times should be derived from asio::time_traits.

    #include <boost/asio.hpp>
    #include <boost/asio/time_traits.hpp>
    #include <boost/thread.hpp> 
    #include <boost/bind.hpp> 
    #include <boost/date_time/posix_time/posix_time.hpp> 
    #include <iostream> 
    using namespace std;
    
    typedef boost::asio::time_traits<boost::posix_time::ptime> time_traits_t;  
    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 = time_traits_t::now();
            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 should work out for you in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried using the Microsoft ReportingControls but found them overly cumbersome, with too little
I tried using "cap rollback" but I get the following error: $ cap rollback
I've tried using source control for a couple projects but still don't really understand
I have tried using the obvious method as outlined in the following example but
I just got Java5 project that has this error, i tried using Java5 and
Anybody tried here using Getting Real(37Signals) approach to develop windows application? (C#/.NET). Or simply
Have you tried using MVC or any other UI pattern for GWT client code.
Have you tried using straight javascript (rather straight ECMAscript 3 standard) in Flex applications,
I have a program that monitors debug messages and I have tried using a
I was using Codeplex and tried connecting to their source control using Team Explorer,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.