jump.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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 <boost/context/fiber.hpp>
  8. namespace ctx = boost::context;
  9. int main() {
  10. int data = 1;
  11. ctx::fiber f{
  12. [&data](ctx::fiber && f){
  13. std::cout << "entered first time: " << data << std::endl;
  14. data += 2;
  15. f = std::move( f).resume();
  16. std::cout << "entered second time: " << data << std::endl;
  17. return std::move( f);
  18. }};
  19. f = std::move( f).resume();
  20. std::cout << "returned first time: " << data << std::endl;
  21. data += 2;
  22. f = std::move( f).resume();
  23. if ( f) {
  24. std::cout << "returned second time: " << data << std::endl;
  25. } else {
  26. std::cout << "returned second time: execution context terminated" << std::endl;
  27. }
  28. std::cout << "main: done" << std::endl;
  29. return EXIT_SUCCESS;
  30. }