]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/example/future_when_all.cpp
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>
8 #if ! defined BOOST_NO_CXX11_DECLTYPE
9 #define BOOST_RESULT_OF_USE_DECLTYPE
13 #define BOOST_THREAD_VERSION 4
14 //#define BOOST_THREAD_USES_LOG
15 #define BOOST_THREAD_USES_LOG_THREAD_ID
17 #include <boost/thread/future.hpp>
18 #include <boost/thread/csbl/vector.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/thread/detail/log.hpp>
22 #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
27 << "P1" << BOOST_THREAD_END_LOG
;
28 boost::this_thread::sleep_for(boost::chrono::seconds(1));
34 << "P1b" << BOOST_THREAD_END_LOG
;
35 boost::this_thread::sleep_for(boost::chrono::seconds(1));
39 int p2(boost::future
<int> f
)
42 << " P2 " << BOOST_THREAD_END_LOG
;
47 catch (std::exception
& ex
)
50 << "ERRORRRRR " << ex
.what() << "" << BOOST_THREAD_END_LOG
;
56 << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG
;
60 << "P2>" << BOOST_THREAD_END_LOG
;
64 int p2s(boost::shared_future
<int> f
)
67 << "<P2" << BOOST_THREAD_END_LOG
;
72 catch (std::exception
& ex
)
75 << "ERRORRRRR " << ex
.what() << "" << BOOST_THREAD_END_LOG
;
81 << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG
;
85 << "P2>" << BOOST_THREAD_END_LOG
;
92 << "<MAIN" << BOOST_THREAD_END_LOG
;
97 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
98 boost::future
<boost::csbl::tuple
<> > all0
= boost::when_all();
100 << BOOST_THREAD_END_LOG
;
103 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
104 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
105 boost::future
<boost::csbl::tuple
<boost::future
<int> > > all
= boost::when_all(boost::move(f1
));
106 boost::csbl::tuple
<boost::future
<int> > res
= all
.get();
108 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
110 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
112 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
113 boost::future
<int> f1
= boost::async(boost::launch::deferred
, &p1
);
114 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
115 boost::future
<boost::csbl::tuple
<boost::future
<int> > > all
= boost::when_all(boost::move(f1
));
116 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
117 boost::csbl::tuple
<boost::future
<int> > res
= all
.get();
119 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
123 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
124 boost::future
<int> f1
= boost::make_ready_future(1);
125 boost::future
<boost::csbl::tuple
<boost::future
<int> > > all
= boost::when_all(boost::move(f1
));
126 boost::csbl::tuple
<boost::future
<int> > res
= all
.get();
128 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
131 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
132 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
133 boost::future
<int> f2
= boost::async(boost::launch::async
, &p1b
);
134 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_all(boost::move(f1
), boost::move(f2
));
136 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
138 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
140 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
143 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
144 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
145 boost::future
<std::string
> f2
= boost::make_ready_future(std::string("nnnnnnn"));;
146 boost::future
<boost::csbl::tuple
<boost::future
<int>, boost::future
<std::string
> > > all
= boost::when_all(boost::move(f1
), boost::move(f2
));
148 boost::csbl::tuple
<boost::future
<int>, boost::future
<std::string
> > res
= all
.get();
150 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
152 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
155 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
156 boost::csbl::vector
<boost::future
<int> > v
;
157 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
158 v
.push_back(boost::async(boost::launch::async
, &p1
));
159 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
160 v
.push_back(boost::async(boost::launch::async
, &p1b
));
161 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
162 boost::future
<boost::csbl::vector
<boost::future
<int> > > all
= boost::when_all(v
.begin(), v
.end());
163 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
164 boost::csbl::vector
<boost::future
<int> > res
= all
.get();
166 << res
[0].get() <<" " << BOOST_THREAD_END_LOG
;
168 << res
[1].get() <<" " << BOOST_THREAD_END_LOG
;
171 catch (std::exception
& ex
)
174 << "ERRORRRRR " << ex
.what() << "" << BOOST_THREAD_END_LOG
;
180 << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG
;
188 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
189 boost::future
<boost::csbl::tuple
<> > all0
= boost::when_any();
190 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
193 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
194 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
195 boost::future
<boost::csbl::tuple
<boost::future
<int> > > all
= boost::when_any(boost::move(f1
));
196 boost::csbl::tuple
<boost::future
<int> > res
= all
.get();
198 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
200 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
202 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
203 boost::future
<int> f1
= boost::async(boost::launch::deferred
, &p1
);
204 boost::future
<boost::csbl::tuple
<boost::future
<int> > > all
= boost::when_any(boost::move(f1
));
205 boost::csbl::tuple
<boost::future
<int> > res
= all
.get();
207 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
211 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
212 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
213 boost::future
<int> f2
= boost::async(boost::launch::async
, &p1b
);
214 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
215 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
217 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
219 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
222 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
223 boost::future
<int> f1
= boost::make_ready_future(1);
224 boost::future
<int> f2
= boost::async(boost::launch::async
, &p1b
);
225 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
226 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
228 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
230 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
233 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
234 boost::future
<std::string
> f1
= boost::make_ready_future(std::string("aaaa"));
235 boost::future
<int> f2
= boost::async(boost::launch::async
, &p1b
);
236 boost::future
<boost::csbl::tuple
<boost::future
<std::string
>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
237 boost::csbl::tuple
<boost::future
<std::string
>,boost::future
<int> > res
= all
.get();
239 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
241 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
244 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
245 boost::future
<int> f2
= boost::make_ready_future(1);
246 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1b
);
247 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
248 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
250 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
252 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
254 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
256 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
257 boost::future
<int> f1
= boost::async(boost::launch::deferred
, &p1
);
258 boost::future
<int> f2
= boost::async(boost::launch::async
, &p1b
);
259 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
260 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
262 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
264 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
267 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
268 boost::future
<int> f1
= boost::async(boost::launch::async
, &p1
);
269 boost::future
<int> f2
= boost::async(boost::launch::deferred
, &p1b
);
270 boost::future
<boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > > all
= boost::when_any(boost::move(f1
), boost::move(f2
));
271 boost::csbl::tuple
<boost::future
<int>,boost::future
<int> > res
= all
.get();
273 << boost::csbl::get
<0>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
275 << boost::csbl::get
<1>(res
).get() <<" " << BOOST_THREAD_END_LOG
;
279 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
280 boost::csbl::vector
<boost::future
<int> > v
;
281 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
282 v
.push_back(boost::async(boost::launch::async
, &p1
));
283 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
284 v
.push_back(boost::async(boost::launch::async
, &p1b
));
285 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
286 boost::future
<boost::csbl::vector
<boost::future
<int> > > all
= boost::when_any(v
.begin(), v
.end());
287 BOOST_THREAD_LOG
<< BOOST_THREAD_END_LOG
;
288 boost::csbl::vector
<boost::future
<int> > res
= all
.get();
290 << res
[0].get() <<" " << BOOST_THREAD_END_LOG
;
292 << res
[1].get() <<" " << BOOST_THREAD_END_LOG
;
295 catch (std::exception
& ex
)
298 << "ERRORRRRR " << ex
.what() << "" << BOOST_THREAD_END_LOG
;
304 << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG
;
309 << "MAIN>" << BOOST_THREAD_END_LOG
;
313 #include <boost/thread/csbl/vector.hpp>
314 using namespace boost
;
316 void f( boost::csbl::vector
<future
<int> > &//vec
317 , BOOST_THREAD_RV_REF(future
<int>) //f
322 boost::csbl::vector
<future
<int> > vec
;
323 f(vec
, make_ready_future(0));