diff --git a/examples/asio/echo_server.cpp b/examples/asio/echo_server.cpp index 7008d624..ff6a7704 100644 --- a/examples/asio/echo_server.cpp +++ b/examples/asio/echo_server.cpp @@ -118,14 +118,13 @@ int main( int argc, char* argv[]) { std::cout << "fiber " << id << " : shutdown" << std::endl; // stop io_service io_svc.stop(); - // interrupt acceptor fiber + // interrupt f.interrupt(); + f.detach(); std::cout << "fiber " << id << " terminates" << std::endl; }).detach(); // run io_service - //io_svc.run(); - boost::fibers::asio::run( io_svc); - f.join(); + io_svc.run(); std::cout << "fiber " << id << " (main-fiber) terminates" << std::endl; std::cout << "done." << std::endl; return EXIT_SUCCESS; diff --git a/examples/asio/round_robin.hpp b/examples/asio/round_robin.hpp index 3c0b5e18..26fe71f8 100644 --- a/examples/asio/round_robin.hpp +++ b/examples/asio/round_robin.hpp @@ -43,7 +43,6 @@ public: struct service : public boost::asio::io_service::service { static boost::asio::io_service::id id; - round_robin * rr{ nullptr }; std::unique_ptr< boost::asio::io_service::work > work_{}; service( boost::asio::io_service & io_svc) : @@ -54,21 +53,8 @@ public: service( service const&) = delete; service & operator=( service const&) = delete; - bool has_ready_fibers() const noexcept { - return rr->has_ready_fibers(); - } - - void poll() { - // block this fiber till all pending (ready) fibers are processed - // == round_robin::suspend_until() has been called - std::unique_lock< boost::fibers::mutex > lk( rr->mtx_); - rr->cnd_.wait( lk); - } - void shutdown_service() override final { work_.reset(); - std::unique_lock< boost::fibers::mutex > lk( rr->mtx_); - rr->cnd_.notify_all(); } }; @@ -76,7 +62,24 @@ public: io_svc_( io_svc), suspend_timer_( io_svc_) { boost::asio::add_service< service >( io_svc_, new service( io_svc_) ); - boost::asio::use_service< service >( io_svc_).rr = this; + io_svc_.post([this]() mutable { + while ( ! io_svc_.stopped() ) { + if ( has_ready_fibers() ) { + // run all pending handlers in round_robin + while ( io_svc_.poll() ); + // block this fiber till all pending (ready) fibers are processed + // == round_robin::suspend_until() has been called + std::unique_lock< boost::fibers::mutex > lk( mtx_); + cnd_.wait( lk); + } else { + // run one handler inside io_service + // if no handler available, block this thread + if ( ! io_svc_.run_one() ) { + break; + } + } + } + }); } void awakened( boost::fibers::context * ctx) noexcept { @@ -123,23 +126,6 @@ public: boost::asio::io_service::id round_robin::service::id; -void run( boost::asio::io_service & io_svc) { - BOOST_ASSERT( boost::asio::has_service< round_robin::service >( io_svc) ); - while ( ! io_svc.stopped() ) { - if ( boost::asio::use_service< round_robin::service >( io_svc).has_ready_fibers() ) { - // run all pending handlers in round_robin - while ( io_svc.poll() ); - boost::asio::use_service< round_robin::service >( io_svc).poll(); - } else { - // run one handler inside io_service - // if no handler available, block this thread - if ( ! io_svc.run_one() ) { - break; - } - } - } -} - }}} #ifdef BOOST_HAS_ABI_HEADERS