Advertisement
venkat_330

Simple timer mechanism with boost

Aug 9th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <timer.h>
  4. #define sleep(n) Sleep(1000 * n)
  5.  
  6. using namespace boost::posix_time;
  7. using namespace std;
  8.  
  9.  
  10. void handler1(int id,const boost::system::error_code &ec)
  11. {
  12.     if (ec == boost::asio::error::operation_aborted)
  13.     {
  14.         std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
  15.         std::cout << "TIMER ID : "  << id<< std::endl;
  16.     }
  17.     else
  18.     {
  19.         std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
  20.        std::cout << "fsm RECIEVE msgBuf : "  << id<< std::endl;
  21.     }
  22. }
  23.  
  24.  
  25.  
  26. Mytimer::Mytimer()
  27.   : m_pTimer(NULL),
  28.     m_pThread(NULL)
  29. {
  30.  
  31.     m_pTimer = new boost::asio::deadline_timer(io_service1);
  32.  
  33. }
  34.  
  35. void Mytimer::runTimerThread()
  36. {
  37.     std::cout << "IO Service started "  << std::endl;
  38.     io_service1.run();
  39. }
  40.  
  41. void Mytimer::startTimer(int time,int timerId)
  42. {
  43.     m_pTimer->expires_from_now(boost::posix_time::seconds(time));
  44.     m_pTimer->async_wait(boost::bind(handler1,timerId,boost::asio::placeholders::error));
  45.     m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
  46. }
  47.  
  48.  
  49. void Mytimer::stopTimer()
  50. {
  51.     io_service1.reset();
  52.     io_service1.stop();
  53. }
  54.  
  55.  
  56. bool Mytimer::isRunning()
  57. {
  58.  
  59.     time_duration td = m_pTimer->expires_from_now();
  60.     if (td.total_seconds() > 0)
  61.     {
  62.         return true;
  63.     }
  64.     return false;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. void TimerAccess::StartAppTimer(int Timerid,int TimerPeriod){
  71.      std::cout << "TIMER ID : "  << Timerid<< std::endl;
  72.     if (Timerid == APPTIMER1){
  73.         timer1.startTimer(TimerPeriod,Timerid);
  74.     }
  75.     if (Timerid == APPTIMER2){
  76.         timer2.startTimer(TimerPeriod,Timerid);
  77.     }
  78.     if (Timerid == APPTIMER3){
  79.         timer3.startTimer(TimerPeriod,Timerid);
  80.     }
  81. }
  82.  
  83. void TimerAccess::StopTimer(int Timerid){
  84.     if (Timerid == APPTIMER1){
  85.         timer1.stopTimer();
  86.     }
  87.     if (Timerid == APPTIMER2){
  88.         timer2.stopTimer();
  89.     }
  90.     if (Timerid == APPTIMER3){
  91.         timer3.stopTimer();
  92.     }
  93.    // return -1;
  94. }
  95.  
  96. int main()
  97. {
  98.  
  99.     cout << " before timer construction" << endl;
  100.      TimerAccess timer;
  101.      timer.StartAppTimer(1,10);
  102.     sleep(15);
  103.     cout << " before starting timer 2" << endl;
  104.     timer.StartAppTimer(1,10);
  105.     sleep(30);
  106.     cout  << " END OF MAIN " << endl;
  107.  
  108. }
  109.  
  110. On the first timer run call  timer.StartAppTimer(1,10); i am getting the handler message but when i rerun the start time i says started but after 10 seconds i receive no handler message..
  111. actual Result:
  112.      before timer construction
  113.     TIMER ID : 1
  114.     IO Service started
  115.     2013-Aug-09 18:12:16.463711 Handler1: expired.
  116.     fsm RECIEVE msgBuf : 1
  117.      before starting timer 2
  118.     TIMER ID : 1
  119.     IO Service started
  120.     END OF MAIN
  121.  
  122. Expected result:
  123.          before timer construction
  124.     TIMER ID : 1
  125.     IO Service started
  126.     2013-Aug-09 18:12:16.463711 Handler1: expired.
  127.     fsm RECIEVE msgBuf : 1
  128.      before starting timer 2
  129.     TIMER ID : 1
  130.     IO Service started
  131.     2013-Aug-09 (some time) Handler1: expired.
  132.     END OF MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement