Skip to content

Instantly share code, notes, and snippets.

@excavador
Created July 19, 2013 18:26
Show Gist options
  • Save excavador/6041294 to your computer and use it in GitHub Desktop.
Save excavador/6041294 to your computer and use it in GitHub Desktop.
#include <boost/utility.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class Repeater : boost::noncopyable
{
public:
typedef bool(*Action)();
Repeater(boost::asio::io_service& io,
Action action,
boost::deadline_timer::duration_type interval) : m_timer(io)
{
}
~Repeater()
{
m_timer.cancel();
m_timer.wait();
}
void start()
{
execute();
}
void stop()
{
m_timer.cancel();
}
private:
void execute()
{
// what if timer already executed?
// Need investigation about behavior boost::asio::deadline_timer.async_wait
m_timer.async_wait(boost::bind(&Repeater::handler, this));
}
void handler(const boost::system::error_code& error)
{
if(error) {
return;
}
if (m_action()) {
execute();
}
}
private:
Action m_action;
boost::asio::deadline_timer m_timer;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment