why is not boost::asio::deadline_timer.wait() blocking in this case?

While trying to understand the difference between wait() and async_wait, I modified the tutorial example a little to test 2 timers at the same time like this: int test_timer() { boost::asio::io_service io; size_t delay1 = 5; size_t delay2 = 3; boost::asio::deadline_timer t1(io, boost::posix_time::seconds(delay1)); boost::asio::deadline_timer t2(io, boost::posix_time::seconds(delay2)); boost::timer t; t2.wait(); std::cout << t.elapsed() << " sec elapsed!\n"; t1.wait(); std::cout << t.elapsed() << " sec elapsed!\n"; return 0; } I got this output: 3.004 sec elapsed! 5.007 sec elapsed! However, I was expecting t2 and t1 to execute sequentially and thus getting: 3.004 sec elapsed! 8 sec elapsed! What was wrong with my understanding about synchronous waiting here? Thanks.

Inner of asio use absolute time in timer queue.The absolute time have been calculated when the deadline_timer be constructing,not when the wait() be executing. When you passing a relative time,it then calculate the absolute time when it will timeout. Member function expires_from_now() of deadline_timer also has same behavior. Sorry for my poor english.
While trying to understand the difference between wait() and async_wait, I modified the tutorial example a little to test 2 timers at the same time like this:
int test_timer()
{
boost::asio::io_service io;
size_t delay1 = 5;
size_t delay2 = 3;
boost::asio::deadline_timer t1(io, boost::posix_time::seconds(delay1));
boost::asio::deadline_timer t2(io, boost::posix_time::seconds(delay2));
boost::timer t;
t2.wait();
std::cout << t.elapsed() << " sec elapsed!\n";
t1.wait();
std::cout << t.elapsed() << " sec elapsed!\n";
return 0;
}
I got this output:
3.004 sec elapsed!
5.007 sec elapsed!
However, I was expecting t2 and t1 to execute sequentially and thus getting:
3.004 sec elapsed!
8 sec elapsed!
What was wrong with my understanding about synchronous waiting here? Thanks.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (2)
-
Qiu Yuzhou
-
Tan, Tom (Shanghai)