1 | // Copyright (C) 2012-2013 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 | #include <iostream> |
11 | |
12 | #define BOOST_THREAD_VERSION 4 |
13 | #define BOOST_THREAD_PROVIDES_EXECUTORS |
14 | //#define BOOST_THREAD_USES_LOG |
15 | #define BOOST_THREAD_USES_LOG_THREAD_ID |
16 | #define BOOST_THREAD_QUEUE_DEPRECATE_OLD |
17 | |
18 | #include <boost/thread/caller_context.hpp> |
19 | #include <boost/thread/executors/basic_thread_pool.hpp> |
20 | #include <boost/thread/executors/loop_executor.hpp> |
21 | #include <boost/thread/executors/serial_executor.hpp> |
22 | #include <boost/thread/executors/inline_executor.hpp> |
23 | #include <boost/thread/executors/thread_executor.hpp> |
24 | #include <boost/thread/executors/executor.hpp> |
25 | #include <boost/thread/executors/executor_adaptor.hpp> |
26 | #include <boost/thread/executor.hpp> |
27 | #include <boost/thread/future.hpp> |
28 | #include <boost/assert.hpp> |
29 | #include <string> |
30 | #include <iostream> |
31 | #include <cassert> |
32 | |
33 | boost::future<void> p(boost::future<void> f) { |
34 | assert(f.is_ready()); |
35 | return boost::make_ready_future(); |
36 | } |
37 | |
38 | void p1() |
39 | { |
40 | // std::cout << BOOST_CONTEXTOF << std::endl; |
41 | //boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); |
42 | } |
43 | |
44 | void p2() |
45 | { |
46 | // std::cout << BOOST_CONTEXTOF << std::endl; |
47 | //boost::this_thread::sleep_for(boost::chrono::seconds(10)); |
48 | } |
49 | |
50 | int f1() |
51 | { |
52 | std::cout << BOOST_CONTEXTOF << std::endl; |
53 | boost::this_thread::sleep_for(d: boost::chrono::seconds(1)); |
54 | std::cout << BOOST_CONTEXTOF << std::endl; |
55 | return 1; |
56 | } |
57 | int f2(int i) |
58 | { |
59 | // std::cout << BOOST_CONTEXTOF << std::endl; |
60 | boost::this_thread::sleep_for(d: boost::chrono::seconds(2)); |
61 | return i + 1; |
62 | } |
63 | |
64 | void submit_some(boost::executor& tp) |
65 | { |
66 | for (int i = 0; i < 3; ++i) { |
67 | tp.submit(closure: &p2); |
68 | } |
69 | for (int i = 0; i < 3; ++i) { |
70 | tp.submit(closure: &p1); |
71 | } |
72 | |
73 | } |
74 | |
75 | |
76 | void at_th_entry(boost::basic_thread_pool& ) |
77 | { |
78 | |
79 | } |
80 | |
81 | int test_executor_adaptor() |
82 | { |
83 | std::cout << BOOST_CONTEXTOF << std::endl; |
84 | { |
85 | try |
86 | { |
87 | { |
88 | boost::executor_adaptor < boost::basic_thread_pool > ea(4); |
89 | std::cout << BOOST_CONTEXTOF << std::endl; |
90 | submit_some( tp&: ea); |
91 | std::cout << BOOST_CONTEXTOF << std::endl; |
92 | #if 1 |
93 | // fixme |
94 | // ERROR= tr1::bad_weak_ptr |
95 | { |
96 | boost::future<int> t1 = boost::async(ex&: ea, f: &f1); |
97 | boost::future<int> t2 = boost::async(ex&: ea, f: &f1); |
98 | std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; |
99 | std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl; |
100 | } |
101 | std::cout << BOOST_CONTEXTOF << std::endl; |
102 | submit_some(tp&: ea); |
103 | std::cout << BOOST_CONTEXTOF << std::endl; |
104 | { |
105 | boost::basic_thread_pool ea3(1); |
106 | std::cout << BOOST_CONTEXTOF << std::endl; |
107 | boost::future<int> t1 = boost::async(ex&: ea3, f: &f1); |
108 | std::cout << BOOST_CONTEXTOF << std::endl; |
109 | boost::future<int> t2 = boost::async(ex&: ea3, f: &f1); |
110 | std::cout << BOOST_CONTEXTOF << std::endl; |
111 | //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11 |
112 | //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98 |
113 | std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; |
114 | std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl; |
115 | } |
116 | #endif |
117 | std::cout << BOOST_CONTEXTOF << std::endl; |
118 | submit_some(tp&: ea); |
119 | std::cout << BOOST_CONTEXTOF << std::endl; |
120 | } |
121 | std::cout << BOOST_CONTEXTOF << std::endl; |
122 | { |
123 | boost::executor_adaptor < boost::loop_executor > ea2; |
124 | submit_some( tp&: ea2); |
125 | ea2.underlying_executor().run_queued_closures(); |
126 | } |
127 | #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) |
128 | std::cout << BOOST_CONTEXTOF << std::endl; |
129 | { |
130 | boost::executor_adaptor < boost::basic_thread_pool > ea1(4); |
131 | boost::executor_adaptor < boost::serial_executor > ea2(ea1); |
132 | submit_some(tp&: ea2); |
133 | } |
134 | #endif |
135 | std::cout << BOOST_CONTEXTOF << std::endl; |
136 | { |
137 | boost::executor_adaptor < boost::inline_executor > ea1; |
138 | submit_some(tp&: ea1); |
139 | } |
140 | std::cout << BOOST_CONTEXTOF << std::endl; |
141 | { |
142 | boost::executor_adaptor < boost::thread_executor > ea1; |
143 | submit_some(tp&: ea1); |
144 | } |
145 | std::cout << BOOST_CONTEXTOF << std::endl; |
146 | #if 1 |
147 | // fixme |
148 | // ERROR= tr1::bad_weak_ptr |
149 | { |
150 | boost::basic_thread_pool ea(4, at_th_entry); |
151 | boost::future<int> t1 = boost::async(ex&: ea, f: &f1); |
152 | std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; |
153 | } |
154 | #endif |
155 | std::cout << BOOST_CONTEXTOF << std::endl; |
156 | { |
157 | boost::async(f: &f1); |
158 | } |
159 | #if 1 |
160 | // fixme |
161 | // ERROR= tr1::bad_weak_ptr |
162 | |
163 | std::cout << BOOST_CONTEXTOF << std::endl; |
164 | { |
165 | boost::basic_thread_pool ea(1); |
166 | boost::async(ex&: ea,f: &f1); |
167 | } |
168 | #endif |
169 | std::cout << BOOST_CONTEXTOF << std::endl; |
170 | boost::this_thread::sleep_for(d: boost::chrono::milliseconds(200)); |
171 | std::cout << BOOST_CONTEXTOF << std::endl; |
172 | } |
173 | catch (std::exception& ex) |
174 | { |
175 | std::cout << "ERROR= " << ex.what() << "" << std::endl; |
176 | return 1; |
177 | } |
178 | catch (...) |
179 | { |
180 | std::cout << " ERROR= exception thrown" << std::endl; |
181 | return 2; |
182 | } |
183 | } |
184 | // std::cout << BOOST_CONTEXTOF << std::endl; |
185 | return 0; |
186 | } |
187 | |
188 | |
189 | int main() |
190 | { |
191 | return test_executor_adaptor(); |
192 | |
193 | #if 0 && defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION \ |
194 | && defined BOOST_THREAD_PROVIDES_EXECUTORS \ |
195 | && ! defined BOOST_NO_CXX11_RVALUE_REFERENCES |
196 | |
197 | boost::basic_thread_pool executor; |
198 | // compiles |
199 | boost::make_ready_future().then(&p); |
200 | |
201 | // ?? |
202 | boost::make_ready_future().then(executor, &p); |
203 | |
204 | // doesn't compile |
205 | boost::make_ready_future().then(executor, &p); |
206 | #endif |
207 | } |
208 | |