test_scheduler.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/thread/executors/scheduler.hpp>
  14. #include <boost/thread/executors/basic_thread_pool.hpp>
  15. #include <boost/chrono/chrono_io.hpp>
  16. #include <iostream>
  17. #include <boost/core/lightweight_test.hpp>
  18. using namespace boost::chrono;
  19. typedef boost::executors::basic_thread_pool thread_pool;
  20. void fn(int x)
  21. {
  22. //std::cout << "[" << __LINE__ << "] " << steady_clock::now() << std::endl;
  23. std::cout << x << std::endl;
  24. }
  25. void test_scheduler(const int n, boost::scheduler<>& sch)
  26. {
  27. for(int i = 1; i <= n; i++)
  28. {
  29. sch.submit_after(boost::bind(fn,i), seconds(i));
  30. sch.submit_after(boost::bind(fn,i), milliseconds(i*100));
  31. }
  32. }
  33. void test_after(const int n, boost::scheduler<>& sch)
  34. {
  35. for(int i = 1; i <= n; i++)
  36. {
  37. sch.after(seconds(i)).submit(boost::bind(fn,i));
  38. sch.after(milliseconds(i*100)).submit(boost::bind(fn,i));
  39. }
  40. }
  41. void test_at(const int n, boost::scheduler<>& sch)
  42. {
  43. for(int i = 1; i <= n; i++)
  44. {
  45. sch.at(steady_clock::now()+seconds(i)).submit(boost::bind(fn,i));
  46. sch.at(steady_clock::now()+milliseconds(i*100)).submit(boost::bind(fn,i));
  47. }
  48. }
  49. void test_on(const int n, boost::scheduler<>& sch, thread_pool& tp)
  50. {
  51. for(int i = 1; i <= n; i++)
  52. {
  53. sch.on(tp).after(seconds(i)).submit(boost::bind(fn,i));
  54. sch.on(tp).after(milliseconds(i*100)).submit(boost::bind(fn,i));
  55. }
  56. }
  57. int main()
  58. {
  59. thread_pool tp(4);
  60. boost::scheduler<> sch;
  61. test_scheduler(5, sch);
  62. test_after(5, sch);
  63. test_at(5, sch);
  64. test_on(5, sch, tp);
  65. boost::this_thread::sleep_for(boost::chrono::seconds(10));
  66. return boost::report_errors();
  67. }