æ¨æ¥ã®è¨äºãboost::asio::deadline_timerは複数のタイマーを持てるãã¯èª¤ãã§ããã
ã¿ã¤ãã¼ã¯ã²ã¨ã¤ã®ã¤ã³ã¹ã¿ã³ã¹ã§ã²ã¨ã¤ããè¨å®ã§ãããè¤æ°è¨å®ã§ããã®ã¯async_waitã«ããè¤æ°ãã³ãã©ã®å¾
æ©ã§ããã
以ä¸ã§ã¯ãon_timerã2åasync_waitãã¦ãã®ã§ã2ç§å¾ã«2åon_timerãå¼ã°ãã¾ãã
#include <iostream> #include <string> #include <ctime> #include <boost/asio.hpp> #include <boost/format.hpp> namespace asio = boost::asio; std::string now() { std::time_t t; std::time(&t); std::tm* st = std::localtime(&t); return (boost::format("%1%/%2%/%3% %4%:%5%:%6% : ") % (1900 + st->tm_year) % (1 + st->tm_mon) % st->tm_mday % st->tm_hour % st->tm_min % st->tm_sec ).str(); } void on_timer(const boost::system::error_code& error) { std::cout << now() << error.message() << std::endl; } int main() { asio::io_service io_service; asio::deadline_timer timer(io_service); std::cout << now() << "start" << std::endl; timer.expires_from_now(boost::posix_time::seconds(2)); timer.async_wait(on_timer); timer.async_wait(on_timer); io_service.run(); }
2011/6/29 9:53:35 : start 2011/6/29 9:53:37 : ãã®æä½ãæ£ããçµäºãã¾ããã 2011/6/29 9:53:37 : ãã®æä½ãæ£ããçµäºãã¾ããã