Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / coroutine / example / asymmetric / unwind.cpp
1
2 //          Copyright Oliver Kowalke 2009.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/coroutine/all.hpp>
8
9 #include <cstdlib>
10 #include <iostream>
11
12 #include <boost/bind.hpp>
13
14 struct X : private boost::noncopyable
15 {
16     X() { std::cout << "X()" << std::endl; }
17     ~X() { std::cout << "~X()" << std::endl; }
18 };
19
20 void fn( boost::coroutines::asymmetric_coroutine< void >::push_type & sink)
21 {
22     X x;
23     int i = 0;
24     while ( true)
25     {
26         std::cout << "fn() : " << ++i << std::endl;
27         sink();
28     }
29 }
30
31 int main( int argc, char * argv[])
32 {
33     {
34         boost::coroutines::asymmetric_coroutine< void >::pull_type source( fn);
35         for ( int k = 0; k < 3; ++k)
36         {
37             source();
38         }
39         std::cout << "destroying coroutine and unwinding stack" << std::endl;
40     }
41
42     std::cout << "\nDone" << std::endl;
43
44     return EXIT_SUCCESS;
45 }