1 // Copyright (C) 2012-2013 Vicente Botet
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)
6 #include <boost/config.hpp>
7 #if ! defined BOOST_NO_CXX11_DECLTYPE
8 #define BOOST_RESULT_OF_USE_DECLTYPE
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
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>
33 boost::future
<void> p(boost::future
<void> f
) {
35 return boost::make_ready_future();
40 // std::cout << BOOST_CONTEXTOF << std::endl;
41 //boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
46 // std::cout << BOOST_CONTEXTOF << std::endl;
47 //boost::this_thread::sleep_for(boost::chrono::seconds(10));
52 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
53 boost::this_thread::sleep_for(boost::chrono::seconds(1));
54 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
59 // std::cout << BOOST_CONTEXTOF << std::endl;
60 boost::this_thread::sleep_for(boost::chrono::seconds(2));
64 void submit_some(boost::executor
& tp
)
66 for (int i
= 0; i
< 3; ++i
) {
69 for (int i
= 0; i
< 3; ++i
) {
76 void at_th_entry(boost::basic_thread_pool
& )
81 int test_executor_adaptor()
83 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
88 boost::executor_adaptor
< boost::basic_thread_pool
> ea(4);
89 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
91 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
94 // ERROR= tr1::bad_weak_ptr
96 boost::future
<int> t1
= boost::async(ea
, &f1
);
97 boost::future
<int> t2
= boost::async(ea
, &f1
);
98 std::cout
<< BOOST_CONTEXTOF
<< " t1= " << t1
.get() << std::endl
;
99 std::cout
<< BOOST_CONTEXTOF
<< " t2= " << t2
.get() << std::endl
;
101 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
103 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
105 boost::basic_thread_pool
ea3(1);
106 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
107 boost::future
<int> t1
= boost::async(ea3
, &f1
);
108 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
109 boost::future
<int> t2
= boost::async(ea3
, &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
;
117 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
119 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
121 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
123 boost::executor_adaptor
< boost::loop_executor
> ea2
;
125 ea2
.underlying_executor().run_queued_closures();
127 #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
128 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
130 boost::executor_adaptor
< boost::basic_thread_pool
> ea1(4);
131 boost::executor_adaptor
< boost::serial_executor
> ea2(ea1
);
135 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
137 boost::executor_adaptor
< boost::inline_executor
> ea1
;
140 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
142 boost::executor_adaptor
< boost::thread_executor
> ea1
;
145 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
148 // ERROR= tr1::bad_weak_ptr
150 boost::basic_thread_pool
ea(4, at_th_entry
);
151 boost::future
<int> t1
= boost::async(ea
, &f1
);
152 std::cout
<< BOOST_CONTEXTOF
<< " t1= " << t1
.get() << std::endl
;
155 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
161 // ERROR= tr1::bad_weak_ptr
163 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
165 boost::basic_thread_pool
ea(1);
166 boost::async(ea
,&f1
);
169 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
170 boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
171 std::cout
<< BOOST_CONTEXTOF
<< std::endl
;
173 catch (std::exception
& ex
)
175 std::cout
<< "ERROR= " << ex
.what() << "" << std::endl
;
180 std::cout
<< " ERROR= exception thrown" << std::endl
;
184 // std::cout << BOOST_CONTEXTOF << std::endl;
191 return test_executor_adaptor();
193 #if 0 && defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION \
194 && defined BOOST_THREAD_PROVIDES_EXECUTORS \
195 && ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
197 boost::basic_thread_pool executor
;
199 boost::make_ready_future().then(&p
);
202 boost::make_ready_future().then(executor
, &p
);
205 boost::make_ready_future().then(executor
, &p
);