public
Created

  • Download Gist
bla.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/deadline_timer.hpp>
 
class TimerThing
{
protected:
boost::asio::deadline_timer* statusTimer_;
boost::thread_group worker_threads_;
 
boost::asio::io_service io_service_;
boost::shared_ptr<boost::asio::io_service::work> work_;
 
public:
TimerThing() {}
 
virtual ~TimerThing(){}
 
void updateStatus(const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
return;
 
std::cout<<"Status Update"<<std::endl;
 
statusTimer_->expires_at(statusTimer_->expires_at() + boost::posix_time::seconds(1));
statusTimer_->async_wait(boost::bind(&TimerThing::updateStatus, this, boost::asio::placeholders::error));
}
void start()
{
statusTimer_=new boost::asio::deadline_timer(io_service_);
boost::shared_ptr<boost::asio::io_service::work> myWork(new boost::asio::io_service::work(io_service_));
work_=myWork;
 
worker_threads_.create_thread( boost::bind( &TimerThing::threadAction, this ) );
 
statusTimer_->expires_at(statusTimer_->expires_at() + boost::posix_time::seconds(1));
statusTimer_->async_wait(boost::bind(&TimerThing::updateStatus, this, boost::asio::placeholders::error));
}
 
void threadAction()
{
io_service_.run();
}
 
void stop()
{
work_.reset();
io_service_.stop();
worker_threads_.join_all();
 
delete statusTimer_;
}
 
};
 
 
int main()
{
TimerThing t;
 
std::string input;
std::cout<<"Press f to stop"<<std::endl;
 
t.start();
 
std::cin>>input;
 
t.stop();
 
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.