From 61a8a8ea89507f90b6c5edb05a72cfd00de92df3 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Mon, 15 Sep 2014 18:14:15 +0800 Subject: [PATCH] optimize the example for asio loop --- examples/cpp03/asio/loop.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/cpp03/asio/loop.hpp b/examples/cpp03/asio/loop.hpp index 27c226ef..32f40b62 100644 --- a/examples/cpp03/asio/loop.hpp +++ b/examples/cpp03/asio/loop.hpp @@ -31,7 +31,14 @@ inline void timer_handler( boost::asio::high_resolution_timer & timer) { inline void run_service( boost::asio::io_service & io_service) { boost::asio::high_resolution_timer timer( io_service, boost::chrono::seconds(0) ); timer.async_wait( boost::bind( timer_handler, boost::ref( timer) ) ); - io_service.run(); + while(true) + { + boost::system::error_code ec; + std::size_t num = io_service.run_one(ec); + if (num == 0) + return; + boost::this_fiber::yield(); + } } }}}