1 // Copyright Arnaud Kapp, Oliver Kowalke 2016 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt or copy at 4 // http://www.boost.org/LICENSE_1_0.txt) 5 6 #include <iostream> 7 #include <memory> 8 #include <thread> 9 10 #include <boost/asio.hpp> 11 12 #include <boost/fiber/all.hpp> 13 #include "round_robin.hpp" 14 15 std::shared_ptr< boost::fibers::unbuffered_channel< int > > c; 16 foo()17void foo() { 18 auto io_ptr = std::make_shared< boost::asio::io_context >(); 19 boost::fibers::use_scheduling_algorithm< boost::fibers::asio::round_robin >( io_ptr); 20 boost::fibers::fiber([io_ptr](){ 21 for ( int i = 0; i < 10; ++i) { 22 std::cout << "push " << i << std::endl; 23 c->push( i); 24 } 25 c->close(); 26 io_ptr->stop(); 27 }).detach(); 28 io_ptr->run(); 29 } 30 bar()31void bar() { 32 auto io_ptr = std::make_shared< boost::asio::io_context >(); 33 boost::fibers::use_scheduling_algorithm< boost::fibers::asio::round_robin >( io_ptr); 34 boost::fibers::fiber([io_ptr](){ 35 try { 36 for (;;) { 37 int i = c->value_pop(); 38 std::cout << "pop " << i << std::endl; 39 } 40 } catch ( std::exception const& e) { 41 std::cout << "exception: " << e.what() << std::endl; 42 } 43 io_ptr->stop(); 44 }).detach(); 45 io_ptr->run(); 46 } 47 main()48int main() { 49 c = std::make_shared< boost::fibers::unbuffered_channel< int > >(); 50 std::thread t1( foo); 51 std::thread t2( bar); 52 t2.join(); 53 t1.join(); 54 std::cout << "done." << std::endl; 55 return 0; 56 } 57