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 8334361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:19:25+00:00 2026-06-09T03:19:25+00:00

Here follows the implementation of a test class wrapping a thread with a timer.

  • 0

Here follows the implementation of a test class wrapping a thread with a timer.
The strange thing is that if the deadline is set to 500 milliseconds it works but if I set it to 1000 milliseconds it does not. What am I doing wrong?

#include "TestTimer.hpp"
#include "../SysMLmodel/Package1/Package1.hpp"

TestTimer::TestTimer(){
    thread = boost::thread(boost::bind(&TestTimer::classifierBehavior,this));
    timer = new      boost::asio::deadline_timer(service,boost::posix_time::milliseconds(1000));
    timer->async_wait(boost::bind(&TestTimer::timerBehavior, this));


};

TestTimer::~TestTimer(){
}

void TestTimer::classifierBehavior(){
 service.run();
};


void TestTimer::timerBehavior(){
std::cout<<"timerBehavior\r";
timer->expires_at(timer->expires_at() + boost::posix_time::milliseconds(1000));
timer->async_wait(boost::bind(&TestTimer::timerBehavior,this));
}

UPDATE 1
I have noticed that the program stucks (or at least the standard output in the console for many seconds, about 30) then a lot of “timerBehavior” strings are printed out together as if they have been queued somewhere.

  • 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-06-09T03:19:27+00:00Added an answer on June 9, 2026 at 3:19 am

    You program might have several problems. From what you have shown, it’s hard to say, if the program stops before the timer had a chance to trigger. And, you do not flush your output, use std::endl, if you want to flush the output after a newline. Third, if your thread is going to run the io_service.run() function, it might be that the thread finds an empty io queue and run() will return immediately. To prevent that, there is a work class, that will prevent this. Here is my example, from you code, that might work as expected:

    #include <boost/asio.hpp>
    #include <boost/thread.hpp>
    #include <iostream>
    
    class TestTimer
    {
    public:
        TestTimer()
            : service()
            , work( service )
            , thread( boost::bind( &TestTimer::classifierBehavior,this ) )
            , timer( service,boost::posix_time::milliseconds( 1000 ) )
        {
            timer.async_wait( boost::bind( &TestTimer::timerBehavior, this ) );
        }
    
        ~TestTimer()
        {
            thread.join();
        }
    private:
        void classifierBehavior()
        {
            service.run();
        }
    
    
        void timerBehavior() {
            std::cout << "timerBehavior" << std::endl;
            timer.expires_at( timer.expires_at() + boost::posix_time::milliseconds( 1000 ) );
            timer.async_wait( boost::bind( &TestTimer::timerBehavior,this ) );
        }
    
        boost::asio::io_service         service;
        boost::asio::io_service::work   work;
        boost::thread                   thread;
        boost::asio::deadline_timer     timer;
    };
    
    int main()
    {
        TestTimer test;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract class that looks as follows. public abstract class Entity<PK extends
I have a singleton class that is instantiated as follows: #import FavoritesManager.h static FavoritesManager
I'm using jquery to submit my form (with id #myForm here) as follows: $(document).ready(function()
Here's some code (full program follows later in the question): template <typename T> T
Here is my situation, I have a string as follows 'a':1 'b':2 'c':3 I
Newbie here! My problem is as follows: I have a dynamically populating ul where
I modified this simple countdown timer form here: https://github.com/lunaryorn/snippets/blob/master/qt4/countdown.py as follows. Now I want
Most posts I have read regarding implementation of copy constructor is that you must
I need to create an integration test that demonstrates successful sending of a UDP
I have created a VSTO plugin with my own RTD implementation that I am

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.