future.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 <exception>
  7. #include <functional>
  8. #include <iostream>
  9. #include <string>
  10. #include <boost/fiber/all.hpp>
  11. inline
  12. int fn( std::string const& str, int n)
  13. {
  14. for ( int i = 0; i < n; ++i)
  15. {
  16. std::cout << i << ": " << str << std::endl;
  17. boost::this_fiber::yield();
  18. }
  19. return n;
  20. }
  21. void start()
  22. {
  23. boost::fibers::future< int > fi(
  24. boost::fibers::async(
  25. std::bind( fn, "abc", 5) ) );
  26. fi.wait();
  27. std::cout << "fn() returned " << fi.get() << std::endl;
  28. }
  29. int main()
  30. {
  31. try
  32. {
  33. boost::fibers::fiber( start).join();
  34. std::cout << "done." << std::endl;
  35. return EXIT_SUCCESS;
  36. }
  37. catch ( std::exception const& e)
  38. { std::cerr << "exception: " << e.what() << std::endl; }
  39. catch (...)
  40. { std::cerr << "unhandled exception" << std::endl; }
  41. return EXIT_FAILURE;
  42. }