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@gmail.com
Web: http://khandelwal.amit.googlepages.com/home
-------------------------------------------------------------------