echo.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. typedef boost::coroutines::asymmetric_coroutine< void >::pull_type pull_coro_t;
  10. typedef boost::coroutines::asymmetric_coroutine< void >::push_type push_coro_t;
  11. void echo( pull_coro_t & source, int i)
  12. {
  13. std::cout << i;
  14. source();
  15. }
  16. void runit( push_coro_t & sink1)
  17. {
  18. std::cout << "started! ";
  19. for ( int i = 0; i < 10; ++i)
  20. {
  21. push_coro_t sink2( boost::bind( echo, _1, i) );
  22. while ( sink2)
  23. sink2();
  24. sink1();
  25. }
  26. }
  27. int main( int argc, char * argv[])
  28. {
  29. {
  30. pull_coro_t source( runit);
  31. while ( source) {
  32. std::cout << "-";
  33. source();
  34. }
  35. }
  36. std::cout << "\nDone" << std::endl;
  37. return EXIT_SUCCESS;
  38. }