• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include <boost/config.hpp>
9 #if ! defined  BOOST_NO_CXX11_DECLTYPE
10 #define BOOST_RESULT_OF_USE_DECLTYPE
11 #endif
12 
13 #define BOOST_THREAD_VERSION 4
14 #define BOOST_THREAD_PROVIDES_EXECUTORS
15 
16 #include <boost/thread/executors/scheduler.hpp>
17 #include <boost/thread/executors/basic_thread_pool.hpp>
18 #include <boost/chrono/chrono_io.hpp>
19 #include <iostream>
20 
21 #include <boost/core/lightweight_test.hpp>
22 
23 using namespace boost::chrono;
24 
25 
26 typedef boost::executors::basic_thread_pool thread_pool;
27 
fn(int x)28 void fn(int x)
29 {
30   //std::cout << "[" << __LINE__ << "] " << steady_clock::now() << std::endl;
31     std::cout << x << std::endl;
32 }
33 
test_scheduler(const int n,boost::scheduler<> & sch)34 void test_scheduler(const int n, boost::scheduler<>& sch)
35 {
36     for(int i = 1; i <= n; i++)
37     {
38       sch.submit_after(boost::bind(fn,i), seconds(i));
39       sch.submit_after(boost::bind(fn,i), milliseconds(i*100));
40     }
41 }
42 
test_after(const int n,boost::scheduler<> & sch)43 void test_after(const int n, boost::scheduler<>& sch)
44 {
45     for(int i = 1; i <= n; i++)
46     {
47       sch.after(seconds(i)).submit(boost::bind(fn,i));
48       sch.after(milliseconds(i*100)).submit(boost::bind(fn,i));
49     }
50 }
51 
test_at(const int n,boost::scheduler<> & sch)52 void test_at(const int n, boost::scheduler<>& sch)
53 {
54     for(int i = 1; i <= n; i++)
55     {
56       sch.at(steady_clock::now()+seconds(i)).submit(boost::bind(fn,i));
57       sch.at(steady_clock::now()+milliseconds(i*100)).submit(boost::bind(fn,i));
58     }
59 }
60 
test_on(const int n,boost::scheduler<> & sch,thread_pool & tp)61 void test_on(const int n, boost::scheduler<>& sch, thread_pool& tp)
62 {
63     for(int i = 1; i <= n; i++)
64     {
65       sch.on(tp).after(seconds(i)).submit(boost::bind(fn,i));
66       sch.on(tp).after(milliseconds(i*100)).submit(boost::bind(fn,i));
67     }
68 }
69 
main()70 int main()
71 {
72   thread_pool tp(4);
73   boost::scheduler<> sch;
74   test_scheduler(5, sch);
75   test_after(5, sch);
76   test_at(5, sch);
77   test_on(5, sch, tp);
78   boost::this_thread::sleep_for(boost::chrono::seconds(10));
79 
80   return boost::report_errors();
81 }
82