test_scheduled_tp.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/bind.hpp>
  14. #include <boost/chrono.hpp>
  15. #include <boost/chrono/chrono_io.hpp>
  16. #include <boost/function.hpp>
  17. #include <boost/thread/executors/scheduled_thread_pool.hpp>
  18. #include <iostream>
  19. #include <boost/core/lightweight_test.hpp>
  20. using namespace boost::chrono;
  21. typedef boost::scheduled_thread_pool scheduled_tp;
  22. void fn(int x)
  23. {
  24. std::cout << x << std::endl;
  25. }
  26. void func(steady_clock::time_point pushed, steady_clock::duration dur)
  27. {
  28. BOOST_TEST(pushed + dur < steady_clock::now());
  29. }
  30. void func2(scheduled_tp* tp, steady_clock::duration d)
  31. {
  32. boost::function<void()> fn = boost::bind(func,steady_clock::now(),d);
  33. tp->submit_after(fn, d);
  34. }
  35. void test_timing(const int n)
  36. {
  37. //This function should take n seconds to execute.
  38. boost::scheduled_thread_pool se(4);
  39. for(int i = 1; i <= n; i++)
  40. {
  41. se.submit_after(boost::bind(fn,i), milliseconds(i*100));
  42. }
  43. boost::this_thread::sleep_for(boost::chrono::seconds(10));
  44. //dtor is called here so all task will have to be executed before we return
  45. }
  46. void test_deque_timing()
  47. {
  48. boost::scheduled_thread_pool se(4);
  49. for(int i = 0; i < 10; i++)
  50. {
  51. steady_clock::duration d = milliseconds(i*100);
  52. boost::function<void()> fn = boost::bind(func,steady_clock::now(),d);
  53. se.submit_after(fn,d);
  54. }
  55. }
  56. void test_deque_multi(const int n)
  57. {
  58. scheduled_tp se(4);
  59. boost::thread_group tg;
  60. for(int i = 0; i < n; i++)
  61. {
  62. steady_clock::duration d = milliseconds(i*100);
  63. //boost::function<void()> fn = boost::bind(func,steady_clock::now(),d);
  64. //tg.create_thread(boost::bind(boost::mem_fn(&scheduled_tp::submit_after), &se, fn, d));
  65. tg.create_thread(boost::bind(func2, &se, d));
  66. }
  67. tg.join_all();
  68. //dtor is called here so execution will block until all the closures
  69. //have been completed.
  70. }
  71. int main()
  72. {
  73. steady_clock::time_point start = steady_clock::now();
  74. test_timing(5);
  75. steady_clock::duration diff = steady_clock::now() - start;
  76. BOOST_TEST(diff > milliseconds(500));
  77. test_deque_timing();
  78. test_deque_multi(4);
  79. test_deque_multi(8);
  80. test_deque_multi(16);
  81. return boost::report_errors();
  82. }