1 // Copyright (C) 2014 Ian Forbed
2 // Copyright (C) 2014 Vicente J. Botet Escriba
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)
8 #include <boost/config.hpp>
9 #if ! defined BOOST_NO_CXX11_DECLTYPE
10 #define BOOST_RESULT_OF_USE_DECLTYPE
13 #define BOOST_THREAD_VERSION 4
14 #define BOOST_THREAD_PROVIDES_EXECUTORS
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>
23 #include <boost/core/lightweight_test.hpp>
25 using namespace boost::chrono
;
27 typedef boost::scheduled_thread_pool scheduled_tp
;
31 std::cout
<< x
<< std::endl
;
34 void func(steady_clock::time_point pushed
, steady_clock::duration dur
)
36 BOOST_TEST(pushed
+ dur
< steady_clock::now());
38 void func2(scheduled_tp
* tp
, steady_clock::duration d
)
40 boost::function
<void()> fn
= boost::bind(func
,steady_clock::now(),d
);
41 tp
->submit_after(fn
, d
);
46 void test_timing(const int n
)
48 //This function should take n seconds to execute.
49 boost::scheduled_thread_pool
se(4);
51 for(int i
= 1; i
<= n
; i
++)
53 se
.submit_after(boost::bind(fn
,i
), milliseconds(i
*100));
55 boost::this_thread::sleep_for(boost::chrono::seconds(10));
56 //dtor is called here so all task will have to be executed before we return
59 void test_deque_timing()
61 boost::scheduled_thread_pool
se(4);
62 for(int i
= 0; i
< 10; i
++)
64 steady_clock::duration d
= milliseconds(i
*100);
65 boost::function
<void()> fn
= boost::bind(func
,steady_clock::now(),d
);
66 se
.submit_after(fn
,d
);
70 void test_deque_multi(const int n
)
73 boost::thread_group tg
;
74 for(int i
= 0; i
< n
; i
++)
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(boost::bind(func2
, &se
, d
));
82 //dtor is called here so execution will block until all the closures
83 //have been completed.
88 steady_clock::time_point start
= steady_clock::now();
90 steady_clock::duration diff
= steady_clock::now() - start
;
91 BOOST_TEST(diff
> milliseconds(500));
96 return boost::report_errors();