Advertisement
venkat_330

Timer with boost

Aug 9th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <boost/asio.hpp>
  2. #include <boost/thread.hpp>
  3. #include <boost/date_time/posix_time/posix_time.hpp>
  4. #include <boost/date_time/posix_time/posix_time_io.hpp>
  5. #include <iostream>
  6. #include <windows.h>
  7. #define sleep(n) Sleep(1000 * n)
  8.  
  9. using namespace boost::posix_time;
  10. using namespace std;
  11.  
  12. void handler1(unsigned int id,const boost::system::error_code &ec)
  13. {
  14.     if (ec == boost::asio::error::operation_aborted)
  15.     {
  16.         std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
  17.         std::cout << "TIMER ID : "  << id<< std::endl;
  18.     }
  19.     else
  20.     {
  21.         std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
  22.        std::cout << "fsm RECIEVE msgBuf : "  << id<< std::endl;
  23.     }
  24. }
  25.  
  26. boost::asio::io_service io_service1;
  27.  
  28. class Mytimer {
  29. public:
  30.     Mytimer(unsigned int);
  31.     unsigned int timerIdact;
  32.     void startTimer(int);
  33.     void runTimerThread();
  34.     bool isRunning();
  35.     int getTimerId();
  36.     void setTimerId(int);
  37. private:
  38.     bool m_isRunning;
  39.     boost::asio::deadline_timer* m_pTimer;
  40.     boost::thread* m_pThread;
  41. };
  42.  
  43. Mytimer::Mytimer(unsigned int timerId)
  44.   : m_pTimer(NULL),
  45.     m_pThread(NULL)
  46. {
  47.     timerIdact = timerId;
  48.     m_pTimer = new boost::asio::deadline_timer(io_service1);
  49.     //m_pTimer->async_wait(handler1,timerIdact);
  50.     //m_pTimer->async_wait((boost::bind(handler1, timerIdact));
  51.     m_pTimer->async_wait(boost::bind(handler1,timerIdact,boost::asio::placeholders::error));
  52. }
  53.  
  54. void Mytimer::runTimerThread()
  55. {
  56.     io_service1.run();
  57. }
  58.  
  59. void Mytimer::startTimer(int time)
  60. {
  61.     m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
  62.     m_pTimer->expires_from_now(boost::posix_time::seconds(time));
  63. }
  64.  
  65. bool Mytimer::isRunning()
  66. {
  67.  
  68.     time_duration td = m_pTimer->expires_from_now();
  69.     if (td.total_seconds() > 0)
  70.     {
  71.         return true;
  72.     }
  73.     return false;
  74. }
  75. /*
  76. void Mytimer::setTimerId(int id)
  77. {
  78.     this.timerId = id;
  79.     //timerId = id;
  80.    // return true;
  81. }
  82.  
  83. int Mytimer::getTimerId()
  84. {
  85.     //timerId = id;
  86.     return this -> timerId;
  87. }
  88. */
  89. /*
  90. int StartTimer(int Timerid,int TimerPeriod){
  91.     if (Timerid == APPTIMER1){
  92.         Mytimer timer1;
  93.         timer3.startTimer(TimerPeriod);
  94.         return 0;
  95.     }
  96.     if (Timerid == APPTIMER2){
  97.         Mytimer timer2;
  98.         timer3.startTimer(TimerPeriod);
  99.         return 0;
  100.     }
  101.     if (Timerid == APPTIMER1){
  102.         Mytimer timer3;
  103.         timer3.startTimer(TimerPeriod);
  104.         return 0;
  105.     }
  106. }*/
  107. int main()
  108. {
  109.     time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f");
  110.     std::cout.imbue(std::locale(std::cout.getloc(), facet));
  111.     Mytimer timer3(10);
  112.    // timer3.setTimerId(1);
  113.     timer3.startTimer(15);
  114.  
  115.     cout << microsec_clock::local_time() << " before timer construction" << endl;
  116.     cout << microsec_clock::local_time() << " before startTimer()" << endl;
  117.     cout << microsec_clock::local_time() << " IsRunning: " << timer3.isRunning() << endl;
  118.     for (int i = 0; i <= 20; i++)
  119.     {
  120.         sleep(1);
  121.         cout << microsec_clock::local_time() << " IsRunning: " << timer3.isRunning() << endl;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement