1 2 // Copyright Oliver Kowalke 2016. 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 <iostream> 10 #include <stdexcept> 11 #include <string> 12 13 #include <boost/context/fiber.hpp> 14 15 namespace ctx = boost::context; 16 17 struct my_exception : public std::runtime_error { 18 ctx::fiber f; my_exceptionmy_exception19 my_exception( ctx::fiber && f_, std::string const& what) : 20 std::runtime_error{ what }, 21 f{ std::move( f_) } { 22 } 23 }; 24 main()25int main() { 26 ctx::fiber f{[](ctx::fiber && f) ->ctx::fiber { 27 std::cout << "entered" << std::endl; 28 try { 29 f = std::move( f).resume(); 30 } catch ( my_exception & ex) { 31 std::cerr << "my_exception: " << ex.what() << std::endl; 32 return std::move( ex.f); 33 } 34 return {}; 35 }}; 36 f = std::move( f).resume(); 37 f = std::move( f).resume_with([](ctx::fiber && f) ->ctx::fiber { 38 throw my_exception(std::move( f), "abc"); 39 return {}; 40 }); 41 42 std::cout << "main: done" << std::endl; 43 44 return EXIT_SUCCESS; 45 } 46