asio async_wait() function question

I am reading asio tutorial. Cannot understand the usage of async_wait() function on Timer.3 sample. http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tuttimer3/... ////////////////// void print(const boost::system::error_code& /*e*/, boost::asio::deadline_timer* t, int* count) { if (*count < 5) { std::cout << *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; 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)); io.run(); std::cout << "Final count is " << count << "\n"; return 0; } ///////////////// in the main(), the async_wait() will not be invoked until the io.run() is called. But inside the print() there is a async_wait() call. Why we don't need run() to trigger it? thanks,

in the main(), the async_wait() will not be invoked until the io.run() is called.
But inside the print() there is a async_wait() call. Why we don't need run() to trigger it?
The handler you supply to async_wait() will be invoked during the io_service::run() cycle. Meanwhile it's just queued.
participants (2)
-
Igor R
-
Longyu Mei