simple.cpp 968 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 <memory>
  8. #include <string>
  9. #include <thread>
  10. #include <boost/intrusive_ptr.hpp>
  11. #include <boost/fiber/all.hpp>
  12. inline
  13. void fn( std::string const& str, int n) {
  14. for ( int i = 0; i < n; ++i) {
  15. std::cout << i << ": " << str << std::endl;
  16. boost::this_fiber::yield();
  17. }
  18. }
  19. int main() {
  20. try {
  21. boost::fibers::fiber f1( fn, "abc", 5);
  22. std::cerr << "f1 : " << f1.get_id() << std::endl;
  23. f1.join();
  24. std::cout << "done." << std::endl;
  25. return EXIT_SUCCESS;
  26. } catch ( std::exception const& e) {
  27. std::cerr << "exception: " << e.what() << std::endl;
  28. } catch (...) {
  29. std::cerr << "unhandled exception" << std::endl;
  30. }
  31. return EXIT_FAILURE;
  32. }