Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / coroutine / example / asymmetric / echo.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 typedef boost::coroutines::asymmetric_coroutine< void >::pull_type pull_coro_t;
15 typedef boost::coroutines::asymmetric_coroutine< void >::push_type push_coro_t;
16
17 void echo( pull_coro_t & source, int i)
18 {
19     std::cout << i;
20     source();
21 }
22
23 void runit( push_coro_t & sink1)
24 {
25     std::cout << "started! ";
26     for ( int i = 0; i < 10; ++i)
27     {
28         push_coro_t sink2( boost::bind( echo, _1, i) );
29         while ( sink2)
30             sink2();
31         sink1();
32     }
33 }
34
35 int main( int argc, char * argv[])
36 {
37     {
38         pull_coro_t source( runit);
39         while ( source) {
40             std::cout << "-";
41             source();
42         }
43     }
44
45     std::cout << "\nDone" << std::endl;
46
47     return EXIT_SUCCESS;
48 }