summaryrefslogtreecommitdiff
path: root/libs/context/example/execution_context/fibonacci.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/context/example/execution_context/fibonacci.cpp')
-rw-r--r--libs/context/example/execution_context/fibonacci.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/libs/context/example/execution_context/fibonacci.cpp b/libs/context/example/execution_context/fibonacci.cpp
new file mode 100644
index 000000000..7424fef89
--- /dev/null
+++ b/libs/context/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 <cstdlib>
+#include <iostream>
+
+#include <boost/context/all.hpp>
+
+#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<<p<<std::endl;
+ }
+
+ std::cout << "main: done" << std::endl;
+}