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