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
23using namespace boost::chrono;
24
25
26typedef boost::executors::basic_thread_pool thread_pool;
27
28void fn(int x)
29{
30 //std::cout << "[" << __LINE__ << "] " << steady_clock::now() << std::endl;
31 std::cout << x << std::endl;
32}
33
34void test_scheduler(const int n, boost::scheduler<>& sch)
35{
36 for(int i = 1; i <= n; i++)
37 {
38 sch.submit_after(w: boost::bind(f: fn,a1: i), dura: seconds(i));
39 sch.submit_after(w: boost::bind(f: fn,a1: i), dura: milliseconds(i*100));
40 }
41}
42
43void test_after(const int n, boost::scheduler<>& sch)
44{
45 for(int i = 1; i <= n; i++)
46 {
47 sch.after(rel_time: seconds(i)).submit(w: boost::bind(f: fn,a1: i));
48 sch.after(rel_time: milliseconds(i*100)).submit(w: boost::bind(f: fn,a1: i));
49 }
50}
51
52void test_at(const int n, boost::scheduler<>& sch)
53{
54 for(int i = 1; i <= n; i++)
55 {
56 sch.at(tp: steady_clock::now()+seconds(i)).submit(w: boost::bind(f: fn,a1: i));
57 sch.at(tp: steady_clock::now()+milliseconds(i*100)).submit(w: boost::bind(f: fn,a1: i));
58 }
59}
60
61void test_on(const int n, boost::scheduler<>& sch, thread_pool& tp)
62{
63 for(int i = 1; i <= n; i++)
64 {
65 sch.on(ex&: tp).after(rel_time: seconds(i)).submit(w: boost::bind(f: fn,a1: i));
66 sch.on(ex&: tp).after(rel_time: milliseconds(i*100)).submit(w: boost::bind(f: fn,a1: i));
67 }
68}
69
70int main()
71{
72 thread_pool tp(4);
73 boost::scheduler<> sch;
74 test_scheduler(n: 5, sch);
75 test_after(n: 5, sch);
76 test_at(n: 5, sch);
77 test_on(n: 5, sch, tp);
78 boost::this_thread::sleep_for(d: boost::chrono::seconds(10));
79
80 return boost::report_errors();
81}
82

source code of boost/libs/thread/test/test_scheduler.cpp