Boost logo

Boost Users :

Subject: [Boost-users] Periodic Timer
From: amit khandelwal (khandelwal.amit_at_[hidden])
Date: 2008-09-09 13:36:57


I see that we have a deadline timer in boost. I am looking for a periodic
timer. I see that there is a sample code in boost website to simulate a
periodic timer. Over a period of time I think it will probably cause some
slippage. I am looking at microsecond granularity Can someone please advise?

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

class printer
{
public:
  printer(boost::asio::io_service& io)
    : timer1_(io, boost::posix_time::microseconds(10)),
      count_(0)
  {
    timer1_.async_wait(boost::bind(&printer::print1, this));
  }

  ~printer()
  {
    std::cout << "Final count is " << count_ << "\n";
  }

  void print1()
  {
    if (count_ < 100)
    {
      timer1_.expires_at(timer1_.expires_at() +
boost::posix_time::microseconds(10));
      timer1_.async_wait(boost::bind(&printer::print1, this));
      std::cout << "Timer 1: " << count_ << "\n";
      ++count_;

    }
  }

private:
  boost::asio::deadline_timer timer1_;
  int count_;
};

int main()
{
  boost::asio::io_service io;
  printer p(io);
  boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
  io.run();
  t.join();

  return 0;
}

-- 
-------------------------------------------------------------------
Ph : (732) 647 5679
Email: khandelwal.amit_at_[hidden]
Web: http://khandelwal.amit.googlepages.com/home
-------------------------------------------------------------------


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