unwind.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Oliver Kowalke 2009.
  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 <boost/coroutine/all.hpp>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <boost/bind.hpp>
  9. struct X : private boost::noncopyable
  10. {
  11. X() { std::cout << "X()" << std::endl; }
  12. ~X() { std::cout << "~X()" << std::endl; }
  13. };
  14. typedef boost::coroutines::symmetric_coroutine< void > coro_t;
  15. coro_t::call_type * c1 = 0;
  16. coro_t::call_type * c2 = 0;
  17. void foo( coro_t::yield_type & yield)
  18. {
  19. X x;
  20. std::cout << "foo() entered" << std::endl;
  21. yield( * c2);
  22. yield( * c2);
  23. std::cout << "foo() finished" << std::endl;
  24. }
  25. void bar( coro_t::yield_type & yield)
  26. {
  27. std::cout << "bar() entered" << std::endl;
  28. yield( * c1);
  29. std::cout << "bar() finished" << std::endl;
  30. }
  31. int main( int argc, char * argv[])
  32. {
  33. coro_t::call_type coro1( foo);
  34. coro_t::call_type coro2( bar);
  35. c1 = & coro1;
  36. c2 = & coro2;
  37. coro1();
  38. std::cout << "Done" << std::endl;
  39. return EXIT_SUCCESS;
  40. }