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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:23:49+00:00 2026-05-31T11:23:49+00:00

In my project every class object has its own thread with infinite cycle (while(1))

  • 0

In my project every class object has its own thread with infinite cycle (while(1)) inside, in which particular object functions are performed. And I’m trying to change this so that every object would perform its functions asynchronously with timers.

Basically this is how it works with thread with infinite loop:

class obj
{
    public:
          obj();
          ~obj();
         //custom functions and variables
        int x;
        int obj_action;
        move();
        wait();

}

obj()
{
     obj_action=1;
     //when constructing object, make its thread with infinite while cycle
     boost::thread make_thread(boost::bind(&obj::obj_engine,this));
}

void obj::obj_engine()
{
     while(true)
     {
       if (obj_action==1){move();}
       else if (obj_action==2){wait();}
       Sleep(1);
     }
}

void obj::move()
{
      x++;
      obj_action==2;
}


void obj::wait()
{
      Sleep(5000);
      obj_action==1;
}

This example shows, the obj class, which has constructor , destructor, couple of variables and couple of functions.
When constructing an object (obj()), thread is made. Thread contains a function “obj_engine” , which has infinite loop (while(true)). In the loop there is two functions:
1. wait() – makes a thread sleep for 5 seconds.
2. walk() – simply x+1
those 2 functions switches each other after its end by defining obj_action.

Now I want to change this to, when the constructing and object , asynchronously move() function would be performed, and after move() function, asynchronously wait() function would be performed, and vice verse. So I wouldn’t need to use any threads.

I hoping for result like this:

//constructing
obj()
{
       asynchronous(walk());
}

walk()
{
    x++
    asynchronous(wait());
}

wait()
{
    Sleep(5000);
    asynchronous(walk());
}

I heard you can do this, with boost::asio timers , but I really don’t know how.
I would be very grateful if someone would show me how.

  • 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-31T11:23:50+00:00Added an answer on May 31, 2026 at 11:23 am

    Here you go:

    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/bind.hpp>
    #include <iostream>
    
    
    
    class obj {
    public:
        obj() : x_(0), t_(io_service_, boost::posix_time::seconds(5)) {
            t_.async_wait(boost::bind(&obj::move, this));
            io_service_.run();
        }
    
        void move() {
            x_++;
            std::cout << x_ << std::endl;
            t_.expires_at(t_.expires_at() + boost::posix_time::seconds(5));
            t_.async_wait(boost::bind(&obj::move, this));
        }
    
    private:
        int x_;
        boost::asio::io_service io_service_;
        boost::asio::deadline_timer t_;
    };
    
    int main(int, char**) {
        obj a;
        while(true);
    }
    

    Basically everything you need is covered by the asio tutorial: this tutorial shows you how to use an asynchronous timer, and this tutorial shows you how to reset your timer.

    Update:

    Please use the above source code instead of my initial one – due to the repetitive calls of io_service_.run(), each move call would be called in another thread and after some time your application would crash because of it. The above code fixes this problem, and gets rid of the wait function by doing so.

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

Sidebar

Related Questions

Given that every software project only has so many programmer-hours dedicated to it, how
I have a Django project whereby every model inherits from a common Object model
Every project invariably needs some type of reporting functionality. From a foreach loop in
In every project, i always build up data entry forms for the Entities. I
In virtually every project I've ever worked on, there will be one or two
I'm running an open source project and every now and then Chinese users report
It seems to be added automagically in every project I create and I compile
problem is that I have to checkout a different branch for every project. and
Duplicate post, see: When do you use the "this" keyword? On almost every project
I use log4net in just about every code project. I was wondering what other

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.