ぷろあくた\(^o^)/

Boost.Asio はもうちょっと↓みたいに proactor な部分で遊ぶのを前面に押し出してほしい気がします的な.
要 Boost 1.36.0 以上.少なくとも Windows Vista 64bit (ただし WIN32 の configuration でビルド) + MSVC 8.0 では動いたような気がする.改行をコンソールに入力するとプログラムが終了するような気もします.

#define WIN32_LEAN_AND_MEAN
#include <cstdio>
#include <iostream>

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


class Handler
{
public:
  explicit Handler(boost::asio::io_service &proactor)
    : proactor_(proactor),
      p_timer1_(new boost::asio::deadline_timer(proactor_)),
      p_timer2_(new boost::asio::deadline_timer(proactor_)),
      p_timer3_(new boost::asio::deadline_timer(proactor_)),
      i_(1),
      b_(false)
  {
    p_timer1_->expires_from_now(boost::posix_time::milliseconds(1200));
    p_timer1_->async_wait(boost::bind(&Handler::onTime1, this));
    p_timer2_->expires_from_now(boost::posix_time::seconds(3));
    p_timer2_->async_wait(boost::bind(&Handler::onTime2, this));
    p_timer3_->expires_from_now(boost::posix_time::milliseconds(5100));
    p_timer3_->async_wait(boost::bind(&Handler::onTime3, this));
    proactor_.run();
  }

  void onTime1()
  {
    p_timer1_->expires_from_now(boost::posix_time::seconds(1));
    p_timer1_->async_wait(boost::bind(&Handler::onTime1, this));

    if (!b_) {
      std::cout << i_;
    }
    std::cout << std::endl;
    ++i_;
    b_ = false;
  }

  void onTime2()
  {
    p_timer2_->expires_from_now(boost::posix_time::seconds(3));
    p_timer2_->async_wait(boost::bind(&Handler::onTime2, this));
    std::cout << "Fizz";
    b_ = true;
  }

  void onTime3()
  {
    p_timer3_->expires_from_now(boost::posix_time::seconds(5));
    p_timer3_->async_wait(boost::bind(&Handler::onTime3, this));
    std::cout << "Buzz";
    b_ = true;
  }

private:
  boost::asio::io_service &proactor_;
  boost::shared_ptr<boost::asio::deadline_timer> p_timer1_;
  boost::shared_ptr<boost::asio::deadline_timer> p_timer2_;
  boost::shared_ptr<boost::asio::deadline_timer> p_timer3_;
  int i_;
  bool b_;
}; // class Handler



void threadMain(boost::asio::io_service &io_service)
{
  Handler h(io_service);
}



int main()
{
  boost::asio::io_service io_service;
  boost::thread th(boost::bind(&threadMain, boost::ref(io_service)));
  std::fgetc(stdin);
  io_service.stop();
  th.join();
}

とりあえずシングルスレッドなのに非同期っぽい!という感じは出せているようないないような,っていうかおーえすさんが非同期な部分をごにょごにょがんばってくれているからアプリケーションレベルで楽できるわけでありまして?
っていうか別にこんなことをしたかったわけでは…….