unwind.cpp 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. void fn( boost::coroutines::asymmetric_coroutine< void >::push_type & sink)
  15. {
  16. X x;
  17. int i = 0;
  18. while ( true)
  19. {
  20. std::cout << "fn() : " << ++i << std::endl;
  21. sink();
  22. }
  23. }
  24. int main( int argc, char * argv[])
  25. {
  26. {
  27. boost::coroutines::asymmetric_coroutine< void >::pull_type source( fn);
  28. for ( int k = 0; k < 3; ++k)
  29. {
  30. source();
  31. }
  32. std::cout << "destroying coroutine and unwinding stack" << std::endl;
  33. }
  34. std::cout << "\nDone" << std::endl;
  35. return EXIT_SUCCESS;
  36. }