// // timer.cpp // ~~~~~~~~~ // // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include #include #include #include #include using namespace std; void print(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count) { if (*count < 5) { std::cout << t << " " <<*count << "\n"; ++(*count); t->expires_at(t->expires_at() + boost::posix_time::seconds(1)); t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count)); } } int main() { boost::asio::io_service io,io2; int count = 0; boost::asio::deadline_timer t(io, boost::posix_time::seconds(1)); t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count)); t.expires_at(t.expires_at() + boost::posix_time::seconds(1)); boost::asio::deadline_timer t2(io, boost::posix_time::seconds(1)); t2.async_wait(boost::bind(print, boost::asio::placeholders::error, &t2, &count)); io.run(); io2.run(); std::cout << "Final count is " << count << "\n"; int discard; std::cin>>discard; return 0; }