test_scheduling_adaptor.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2014 Ian Forbed
  2. // Copyright (C) 2014 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #include <boost/config.hpp>
  8. #if ! defined BOOST_NO_CXX11_DECLTYPE
  9. #define BOOST_RESULT_OF_USE_DECLTYPE
  10. #endif
  11. #define BOOST_THREAD_VERSION 4
  12. #define BOOST_THREAD_PROVIDES_EXECUTORS
  13. #include <boost/function.hpp>
  14. #include <boost/thread/executors/executor.hpp>
  15. #include <boost/thread/executors/basic_thread_pool.hpp>
  16. #include <boost/thread/executors/scheduling_adaptor.hpp>
  17. #include <boost/chrono/chrono_io.hpp>
  18. #include <boost/core/lightweight_test.hpp>
  19. using namespace boost::chrono;
  20. typedef boost::executors::basic_thread_pool thread_pool;
  21. void fn(int x)
  22. {
  23. //std::cout << "[" << __LINE__ << "] " << steady_clock::now() << std::endl;
  24. std::cout << x << std::endl;
  25. }
  26. void test_timing(const int n)
  27. {
  28. thread_pool tp(4);
  29. boost::scheduling_adaptor<thread_pool> sa(tp);
  30. for(int i = 1; i <= n; i++)
  31. {
  32. sa.submit_after(boost::bind(fn,i),seconds(i));
  33. sa.submit_after(boost::bind(fn,i), milliseconds(i*100));
  34. }
  35. boost::this_thread::sleep_for(boost::chrono::seconds(10));
  36. }
  37. int main()
  38. {
  39. steady_clock::time_point start = steady_clock::now();
  40. test_timing(5);
  41. steady_clock::duration diff = steady_clock::now() - start;
  42. BOOST_TEST(diff > seconds(5));
  43. return boost::report_errors();
  44. }