From b2af03e76b3a1058d13091f4a6e716ce5af2a135 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Thu, 5 Mar 2015 17:26:19 +0100 Subject: [PATCH] update examples --- example/execution_context/Jamfile.v2 | 8 ++++ example/execution_context/fibonacci.cpp | 36 ++++++++++++++++ example/execution_context/parameter.cpp | 55 +++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 example/execution_context/fibonacci.cpp create mode 100644 example/execution_context/parameter.cpp diff --git a/example/execution_context/Jamfile.v2 b/example/execution_context/Jamfile.v2 index 5810d6f..1b9a610 100644 --- a/example/execution_context/Jamfile.v2 +++ b/example/execution_context/Jamfile.v2 @@ -37,3 +37,11 @@ exe segmented exe parser : parser.cpp ; + +exe fibonacci + : fibonacci.cpp + ; + +exe parameter + : parameter.cpp + ; diff --git a/example/execution_context/fibonacci.cpp b/example/execution_context/fibonacci.cpp new file mode 100644 index 0000000..7424fef --- /dev/null +++ b/example/execution_context/fibonacci.cpp @@ -0,0 +1,36 @@ + +// Copyright Oliver Kowalke 2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +#include + +#define yield(x) p=x; mctx.resume(); + +int main() { + int n=35; + int p=0; + boost::context::execution_context mctx( boost::context::execution_context::current() ); + boost::context::execution_context ctx( + boost::context::fixedsize_stack(), + [n,&p,mctx]()mutable{ + int a=0; + int b=1; + while(n-->0){ + yield(a); + auto next=a+b; + a=b; + b=next; + } + }); + for(int i=0;i<10;++i){ + ctx.resume(); + std::cout< +#include +#include +#include + +#include +#include + +class X{ +private: + int * inp_; + std::string outp_; + std::exception_ptr excptr_; + boost::context::execution_context caller_; + boost::context::execution_context callee_; + +public: + X(): + inp_( nullptr), + outp_(), + excptr_(), + caller_(boost::context::execution_context::current()), + callee_(boost::context::fixedsize_stack(), + [=](){ + try { + int i = * inp_; + outp_ = boost::lexical_cast(i); + caller_.resume(); + } catch (...) { + excptr_=std::current_exception(); + } + }) + {} + + std::string operator()(int i){ + inp_ = & i; + callee_.resume(); + if(excptr_){ + std::rethrow_exception(excptr_); + } + return outp_; + } +}; + +int main() { + X x; + std::cout<