Boost.Asio タイマーループ

deadline_timerではタイマーの繰り返しができなさそうだなーと思っていたら、repeating_timerというのを作っているひとがいました。


http://www.codegorilla.co.uk/wiki/Asio_Repeating_Timer
http://svn.codegorilla.co.uk/repeating_timer/trunk/include/basic_repeating_timer.h

#define WIN32_LEAN_AND_MEAN

#define BOOST_SYSTEM_NO_LIB
#define BOOST_REGEX_NO_LIB

#include <iostream>
#include <windows.h>
#include <shand/boost_thread_header_only.hpp>
#include <boost/asio.hpp>
#include "basic_repeating_timer.hpp"

void handle(const boost::system::error_code& error)
{
    std::cout << "handle" << std::endl;
}

int main()
{
    boost::asio::io_service io_service;

    boost::asio::repeating_timer timer(io_service);
    timer.start(boost::posix_time::seconds(1),
                boost::bind(handle, boost::asio::placeholders::error));

    io_service.run();
    for (;;) {}
}

これを実行すると、1秒ごとに「handle」と出力されます。
これでマルチプラットフォームなタイマー処理が書けます。


# deadline_timerでもできるっぽいです。