parallel.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. void first( boost::coroutines::asymmetric_coroutine< void >::push_type & sink)
  10. {
  11. std::cout << "started first! ";
  12. for ( int i = 0; i < 10; ++i)
  13. {
  14. sink();
  15. std::cout << "a" << i;
  16. }
  17. }
  18. void second( boost::coroutines::asymmetric_coroutine< void >::push_type & sink)
  19. {
  20. std::cout << "started second! ";
  21. for ( int i = 0; i < 10; ++i)
  22. {
  23. sink();
  24. std::cout << "b" << i;
  25. }
  26. }
  27. int main( int argc, char * argv[])
  28. {
  29. {
  30. boost::coroutines::asymmetric_coroutine< void >::pull_type source1( boost::bind( first, _1) );
  31. boost::coroutines::asymmetric_coroutine< void >::pull_type source2( boost::bind( second, _1) );
  32. while ( source1 && source2) {
  33. source1();
  34. std::cout << " ";
  35. source2();
  36. std::cout << " ";
  37. }
  38. }
  39. std::cout << "\nDone" << std::endl;
  40. return EXIT_SUCCESS;
  41. }