1// Copyright (C) 2014 Vicente Botet
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#include <boost/config.hpp>
7#if ! defined BOOST_NO_CXX11_DECLTYPE
8#define BOOST_RESULT_OF_USE_DECLTYPE
9#endif
10
11#define BOOST_THREAD_VERSION 4
12#define BOOST_THREAD_PROVIDES_EXECUTORS
13//#define BOOST_THREAD_USES_LOG
14#define BOOST_THREAD_USES_LOG_THREAD_ID
15#define BOOST_THREAD_QUEUE_DEPRECATE_OLD
16
17#include <boost/thread/caller_context.hpp>
18#include <boost/thread/executors/basic_thread_pool.hpp>
19#include <boost/thread/executors/loop_executor.hpp>
20#include <boost/thread/executors/serial_executor.hpp>
21#include <boost/thread/executors/inline_executor.hpp>
22#include <boost/thread/executors/thread_executor.hpp>
23#include <boost/thread/executors/executor.hpp>
24#include <boost/thread/executors/executor_adaptor.hpp>
25#include <boost/thread/executor.hpp>
26#include <boost/thread/future.hpp>
27#include <boost/assert.hpp>
28#include <string>
29#include <iostream>
30
31void p1()
32{
33 // std::cout << BOOST_CONTEXTOF << std::endl;
34 //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
35}
36
37void p2()
38{
39 // std::cout << BOOST_CONTEXTOF << std::endl;
40 //boost::this_thread::sleep_for(boost::chrono::seconds(10));
41}
42
43int f1()
44{
45 // std::cout << BOOST_CONTEXTOF << std::endl;
46 boost::this_thread::sleep_for(d: boost::chrono::seconds(1));
47 return 1;
48}
49int f2(int i)
50{
51 // std::cout << BOOST_CONTEXTOF << std::endl;
52 boost::this_thread::sleep_for(d: boost::chrono::seconds(2));
53 return i + 1;
54}
55
56void submit_some(boost::generic_executor_ref tp)
57{
58 for (int i = 0; i < 3; ++i) {
59 tp.submit(closure: &p2);
60 }
61 for (int i = 0; i < 3; ++i) {
62 tp.submit(closure: &p1);
63 }
64
65}
66
67void at_th_entry(boost::basic_thread_pool& )
68{
69
70}
71
72
73
74int test_generic_executor_ref()
75{
76 std::cout << BOOST_CONTEXTOF << std::endl;
77 {
78 try
79 {
80 {
81 boost::basic_thread_pool ea(4);
82 submit_some( tp: ea);
83 {
84 boost::future<int> t1 = boost::async(ex&: ea, f: &f1);
85 boost::future<int> t2 = boost::async(ex&: ea, f: &f1);
86 std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
87 std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
88 }
89 submit_some(tp: ea);
90 {
91 boost::basic_thread_pool ea3(1);
92 boost::future<int> t1 = boost::async(ex&: ea3, f: &f1);
93 boost::future<int> t2 = boost::async(ex&: ea3, f: &f1);
94 //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11
95 //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98
96 std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
97 std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl;
98 }
99 submit_some(tp: ea);
100 }
101 std::cout << BOOST_CONTEXTOF << std::endl;
102 {
103 boost::loop_executor ea2;
104 submit_some( tp: ea2);
105 ea2.run_queued_closures();
106 }
107#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
108 std::cout << BOOST_CONTEXTOF << std::endl;
109 {
110 boost::basic_thread_pool ea1(4);
111 boost::serial_executor ea2(ea1);
112 submit_some(tp: ea2);
113 }
114#endif
115 std::cout << BOOST_CONTEXTOF << std::endl;
116 {
117 boost::inline_executor ea1;
118 submit_some(tp: ea1);
119 }
120 std::cout << BOOST_CONTEXTOF << std::endl;
121 {
122 //boost::thread_executor ea1;
123 //submit_some(ea1);
124 }
125 std::cout << BOOST_CONTEXTOF << std::endl;
126 {
127 boost::basic_thread_pool ea(4, at_th_entry);
128 boost::future<int> t1 = boost::async(ex&: ea, f: &f1);
129 std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl;
130 }
131 std::cout << BOOST_CONTEXTOF << std::endl;
132 {
133 boost::basic_thread_pool ea(4, at_th_entry);
134 boost::async(ex&: ea, f: &f1);
135 std::cout << BOOST_CONTEXTOF << std::endl;
136 }
137 std::cout << BOOST_CONTEXTOF << std::endl;
138 boost::this_thread::sleep_for(d: boost::chrono::milliseconds(200));
139 std::cout << BOOST_CONTEXTOF << std::endl;
140
141 }
142 catch (std::exception& ex)
143 {
144 std::cout << "ERROR= " << ex.what() << "" << std::endl;
145 return 1;
146 }
147 catch (...)
148 {
149 std::cout << " ERROR= exception thrown" << std::endl;
150 return 2;
151 }
152 }
153 // std::cout << BOOST_CONTEXTOF << std::endl;
154 return 0;
155}
156
157
158int main()
159{
160 return test_generic_executor_ref();
161
162
163}
164

source code of boost/libs/thread/example/generic_executor_ref.cpp