1 2 // Copyright Oliver Kowalke 2013. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <cstdlib> 8 #include <exception> 9 #include <functional> 10 #include <iostream> 11 #include <string> 12 13 #include <boost/fiber/all.hpp> 14 15 inline fn(std::string const & str,int n)16int fn( std::string const& str, int n) 17 { 18 for ( int i = 0; i < n; ++i) 19 { 20 std::cout << i << ": " << str << std::endl; 21 boost::this_fiber::yield(); 22 } 23 24 return n; 25 } 26 start()27void start() 28 { 29 boost::fibers::future< int > fi( 30 boost::fibers::async( 31 std::bind( fn, "abc", 5) ) ); 32 fi.wait(); 33 std::cout << "fn() returned " << fi.get() << std::endl; 34 } 35 main()36int main() 37 { 38 try 39 { 40 boost::fibers::fiber( start).join(); 41 std::cout << "done." << std::endl; 42 43 return EXIT_SUCCESS; 44 } 45 catch ( std::exception const& e) 46 { std::cerr << "exception: " << e.what() << std::endl; } 47 catch (...) 48 { std::cerr << "unhandled exception" << std::endl; } 49 return EXIT_FAILURE; 50 } 51