Boost.Asioで簡単なバックグラウンド処理

io_service::postで関数を投げる。
io_serviceを一つ作ってしまえば、次々に登録していけるので楽です。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

namespace asio = boost::asio;

void background()
{
    boost::this_thread::sleep(boost::posix_time::seconds(3));
    std::cout << "end" << std::endl;
}

int main()
{
    asio::io_service io_service;
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));

    io_service.post(background);

    for (;;) {
        std::cout << "wait" << std::endl;
        boost::this_thread::sleep(boost::posix_time::seconds(1));
    }

    t.join();
}
wait
wait
wait
end
wait
...

io_service::runをスレッドで実行しないといけないのがよくわかってない。