Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/asio.hpp>
- #include <boost/thread.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/date_time/posix_time/posix_time_io.hpp>
- #include <iostream>
- #include <windows.h>
- #define sleep(n) Sleep(1000 * n)
- using namespace boost::posix_time;
- using namespace std;
- void handler1(unsigned int id,const boost::system::error_code &ec)
- {
- if (ec == boost::asio::error::operation_aborted)
- {
- std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
- std::cout << "TIMER ID : " << id<< std::endl;
- }
- else
- {
- std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
- std::cout << "fsm RECIEVE msgBuf : " << id<< std::endl;
- }
- }
- boost::asio::io_service io_service1;
- class Mytimer {
- public:
- Mytimer(unsigned int);
- unsigned int timerIdact;
- void startTimer(int);
- void runTimerThread();
- bool isRunning();
- int getTimerId();
- void setTimerId(int);
- private:
- bool m_isRunning;
- boost::asio::deadline_timer* m_pTimer;
- boost::thread* m_pThread;
- };
- Mytimer::Mytimer(unsigned int timerId)
- : m_pTimer(NULL),
- m_pThread(NULL)
- {
- timerIdact = timerId;
- m_pTimer = new boost::asio::deadline_timer(io_service1);
- //m_pTimer->async_wait(handler1,timerIdact);
- //m_pTimer->async_wait((boost::bind(handler1, timerIdact));
- m_pTimer->async_wait(boost::bind(handler1,timerIdact,boost::asio::placeholders::error));
- }
- void Mytimer::runTimerThread()
- {
- io_service1.run();
- }
- void Mytimer::startTimer(int time)
- {
- m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
- m_pTimer->expires_from_now(boost::posix_time::seconds(time));
- }
- bool Mytimer::isRunning()
- {
- time_duration td = m_pTimer->expires_from_now();
- if (td.total_seconds() > 0)
- {
- return true;
- }
- return false;
- }
- /*
- void Mytimer::setTimerId(int id)
- {
- this.timerId = id;
- //timerId = id;
- // return true;
- }
- int Mytimer::getTimerId()
- {
- //timerId = id;
- return this -> timerId;
- }
- */
- /*
- int StartTimer(int Timerid,int TimerPeriod){
- if (Timerid == APPTIMER1){
- Mytimer timer1;
- timer3.startTimer(TimerPeriod);
- return 0;
- }
- if (Timerid == APPTIMER2){
- Mytimer timer2;
- timer3.startTimer(TimerPeriod);
- return 0;
- }
- if (Timerid == APPTIMER1){
- Mytimer timer3;
- timer3.startTimer(TimerPeriod);
- return 0;
- }
- }*/
- int main()
- {
- time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f");
- std::cout.imbue(std::locale(std::cout.getloc(), facet));
- Mytimer timer3(10);
- // timer3.setTimerId(1);
- timer3.startTimer(15);
- cout << microsec_clock::local_time() << " before timer construction" << endl;
- cout << microsec_clock::local_time() << " before startTimer()" << endl;
- cout << microsec_clock::local_time() << " IsRunning: " << timer3.isRunning() << endl;
- for (int i = 0; i <= 20; i++)
- {
- sleep(1);
- cout << microsec_clock::local_time() << " IsRunning: " << timer3.isRunning() << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement