simple.cpp 994 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <boost/coroutine/all.hpp>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <boost/bind.hpp>
  5. #include "X.h"
  6. typedef boost::coroutines::asymmetric_coroutine< X& >::pull_type pull_coro_t;
  7. typedef boost::coroutines::asymmetric_coroutine< X& >::push_type push_coro_t;
  8. void fn1( push_coro_t & sink)
  9. {
  10. for ( int i = 0; i < 10; ++i)
  11. {
  12. X x( i);
  13. sink( x);
  14. }
  15. }
  16. void fn2( pull_coro_t & source)
  17. {
  18. while ( source) {
  19. X & x = source.get();
  20. std::cout << "i = " << x.i << std::endl;
  21. source();
  22. }
  23. }
  24. int main( int argc, char * argv[])
  25. {
  26. {
  27. pull_coro_t source( fn1);
  28. while ( source) {
  29. X & x = source.get();
  30. std::cout << "i = " << x.i << std::endl;
  31. source();
  32. }
  33. }
  34. {
  35. push_coro_t sink( fn2);
  36. for ( int i = 0; i < 10; ++i)
  37. {
  38. X x( i);
  39. sink( x);
  40. }
  41. }
  42. std::cout << "Done" << std::endl;
  43. return EXIT_SUCCESS;
  44. }