#include #include #include #include #include #include class Counter { public: Counter():m_value(0) { } void onIncrement() { ++m_value; std::cout<<"Counter is "< event): m_id(id), m_intervalDuration(durationSeconds), m_timer(io,boost::posix_time::seconds(m_intervalDuration)), m_event(event) { std::cerr<<"Creating IntervalEvent id: "< 0) { m_intervalDuration = duration; //reset the async wait m_timer.async_wait(boost::bind(&IntervalEvent::handleTimerExpiration, this,_1)); } else { //Timer already expired, reschedule a wait m_timer.async_wait(boost::bind(&IntervalEvent::handleTimerExpiration, this,_1)); } } void handleTimerExpiration(const boost::system::error_code& error) { //If timer is not cancelled if(error!= boost::asio::error::operation_aborted) { m_event(); handleReset(m_intervalDuration); } } size_t m_id; long m_intervalDuration; boost::asio::deadline_timer m_timer; boost::function m_event; }; int main() { boost::asio::io_service io; boost::shared_ptr work(new boost::asio::io_service::work(io)); boost::thread ioservice_thread(boost::bind(&boost::asio::io_service::run, &io)); std::map > events; Counter count; long duration = 1; size_t timerId = 0; char response = 'Y'; while(response != 'Q') { std::cout<<"Options :"<>response; if(response == 'A') { ++timerId; boost::shared_ptr test(new IntervalEvent(timerId,io, duration, boost::bind(&Counter::onIncrement,boost::ref(count) )) ); events.insert(std::make_pair(timerId,test)); } else if(response == 'M') { size_t id; long duration; std::cout<<"Enter timerid"<>id; std::cout<<"Enter new Duration"<>duration; std::map >::iterator it = events.find(id); if(it!=events.end()) { it->second->reset(duration); } else { std::cerr<<"Cannot find timer id: "<>id; std::map >::iterator it = events.find(id); if(it!=events.end()) { it->second->cancel(); } else { std::cerr<<"Cannot find timer id: "< >::iterator it = events.begin(), itEnd = events.end(); it!=itEnd; ++it) { (it->second)->cancel(); } //reset ioservice_work work.reset(); ioservice_thread.join(); return 0; }