fibonacci.cpp 859 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 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. #include <cstdlib>
  6. #include <iostream>
  7. #include <memory>
  8. #include <boost/context/fiber.hpp>
  9. namespace ctx = boost::context;
  10. int main() {
  11. int a;
  12. ctx::fiber f{
  13. [&a](ctx::fiber && f){
  14. a=0;
  15. int b=1;
  16. for(;;){
  17. f = std::move( f).resume();
  18. int next=a+b;
  19. a=b;
  20. b=next;
  21. }
  22. return std::move( f);
  23. }};
  24. for ( int j = 0; j < 10; ++j) {
  25. f = std::move( f).resume();
  26. std::cout << a << " ";
  27. }
  28. std::cout << std::endl;
  29. std::cout << "main: done" << std::endl;
  30. return EXIT_SUCCESS;
  31. }