test_future_mt_post.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // (C) Copyright 2008-10 Anthony Williams
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <utility>
  7. #include <memory>
  8. #include <stdexcept>
  9. #include <string>
  10. #include <thread>
  11. #include <boost/fiber/all.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. int fn( int i) {
  14. return i;
  15. }
  16. void test_async() {
  17. for ( int i = 0; i < 10; ++i) {
  18. int n = 3;
  19. boost::fibers::packaged_task< int( int) > pt( fn);
  20. boost::fibers::future< int > f( pt.get_future() );
  21. std::thread t(
  22. std::bind(
  23. [n](boost::fibers::packaged_task< int( int) > & pt) mutable -> void {
  24. boost::fibers::fiber( boost::fibers::launch::post, std::move( pt), n).join();
  25. },
  26. std::move( pt) ) );
  27. int result = f.get();
  28. BOOST_CHECK_EQUAL( n, result);
  29. t.join();
  30. }
  31. }
  32. void test_dummy() {}
  33. boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
  34. boost::unit_test_framework::test_suite* test =
  35. BOOST_TEST_SUITE("Boost.Fiber: futures-mt test suite");
  36. #if ! defined(BOOST_FIBERS_NO_ATOMICS)
  37. test->add(BOOST_TEST_CASE(test_async));
  38. #else
  39. test->add(BOOST_TEST_CASE(test_dummy));
  40. #endif
  41. return test;
  42. }