range_for.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Oliver Kowalke 2013.
  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 <cstdlib>
  6. #include <iostream>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <boost/fiber/all.hpp>
  10. typedef boost::fibers::unbuffered_channel< unsigned int > channel_t;
  11. void foo( channel_t & chan) {
  12. chan.push( 1);
  13. chan.push( 1);
  14. chan.push( 2);
  15. chan.push( 3);
  16. chan.push( 5);
  17. chan.push( 8);
  18. chan.push( 12);
  19. chan.close();
  20. }
  21. void bar( channel_t & chan) {
  22. for ( unsigned int value : chan) {
  23. std::cout << value << " ";
  24. }
  25. std::cout << std::endl;
  26. }
  27. int main() {
  28. try {
  29. channel_t chan;
  30. boost::fibers::fiber f1( & foo, std::ref( chan) );
  31. boost::fibers::fiber f2( & bar, std::ref( chan) );
  32. f1.join();
  33. f2.join();
  34. std::cout << "done." << std::endl;
  35. return EXIT_SUCCESS;
  36. } catch ( std::exception const& e) {
  37. std::cerr << "exception: " << e.what() << std::endl;
  38. } catch (...) {
  39. std::cerr << "unhandled exception" << std::endl;
  40. }
  41. return EXIT_FAILURE;
  42. }