1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Copyright (C) 2011 Vicente J. Botet Escriba
12 // Distributed under the Boost Software License, Version 1.0. (See accompanying
13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15 // <boost/thread/future.hpp>
17 // template <class F, class... Args>
18 // future<typename result_of<F(Args...)>::type>
19 // async(F&& f, Args&&... args);
21 // template <class F, class... Args>
22 // future<typename result_of<F(Args...)>::type>
23 // async(launch policy, F&& f, Args&&... args);
25 //#define BOOST_THREAD_VERSION 3
26 #define BOOST_THREAD_VERSION 4
27 #include <boost/config.hpp>
28 #if ! defined BOOST_NO_CXX11_DECLTYPE
29 #define BOOST_RESULT_OF_USE_DECLTYPE
32 #include <boost/thread/future.hpp>
33 #include <boost/thread/thread.hpp>
34 #include <boost/thread/detail/memory.hpp>
35 #include <boost/thread/csbl/memory/unique_ptr.hpp>
37 #include <boost/detail/lightweight_test.hpp>
39 typedef boost::chrono::high_resolution_clock Clock
;
40 typedef boost::chrono::milliseconds ms
;
47 typedef long result_type
;
56 boost::this_thread::sleep_for(ms(200));
59 long operator()() const
61 boost::this_thread::sleep_for(ms(200));
69 typedef int result_type
;
73 BOOST_THREAD_MOVABLE_ONLY(MoveOnly
)
78 MoveOnly( BOOST_THREAD_RV_REF(MoveOnly
))
82 MoveOnly
& operator=(BOOST_THREAD_RV_REF(MoveOnly
))
88 int operator()() const
90 boost::this_thread::sleep_for(ms(200));
93 template <typename OS
>
94 friend OS
& operator<<(OS
& os
, MoveOnly
const& v
)
103 BOOST_THREAD_DCL_MOVABLE (MoveOnly
)
108 boost::this_thread::sleep_for(ms(200));
116 boost::this_thread::sleep_for(ms(200));
122 boost::this_thread::sleep_for(ms(200));
125 boost::csbl::unique_ptr
<int> f3_0()
127 boost::this_thread::sleep_for(ms(200));
128 boost::csbl::unique_ptr
<int> r( (new int(3)));
129 return boost::move(r
);
133 boost::this_thread::sleep_for(ms(200));
135 return boost::move(r
);
138 boost::csbl::unique_ptr
<int> f3(int i
)
140 boost::this_thread::sleep_for(ms(200));
141 return boost::csbl::unique_ptr
<int>(new int(i
));
144 boost::csbl::unique_ptr
<int> f4(
145 BOOST_THREAD_RV_REF_BEG
boost::csbl::unique_ptr
<int> BOOST_THREAD_RV_REF_END p
148 boost::this_thread::sleep_for(ms(200));
149 return boost::move(p
);
153 boost::chrono::nanoseconds delay
;
154 Clock::time_point start
;
155 check_timer(boost::chrono::nanoseconds delay
)
157 , start(Clock::now())
161 Clock::time_point now
= Clock::now();
162 BOOST_TEST(now
- start
< delay
);
163 std::cout
<< __FILE__
<< "[" << __LINE__
<< "] " << (now
- start
).count() << std::endl
;
173 } catch (std::exception
& ex
)
175 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
176 BOOST_TEST(false && "exception thrown");
180 BOOST_TEST(false && "exception thrown");
185 boost::async(boost::launch::async
, f0
);
186 } catch (std::exception
& ex
)
188 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
189 BOOST_TEST(false && "exception thrown");
193 BOOST_TEST(false && "exception thrown");
196 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
197 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
200 boost::async(boost::launch::deferred
, f0
);
201 } catch (std::exception
& ex
)
203 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
204 BOOST_TEST(false && "exception thrown");
208 BOOST_TEST(false && "exception thrown");
212 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
216 boost::future
<int> f
= boost::async(f0
);
217 boost::this_thread::sleep_for(ms(300));
220 check_timer
timer(ms(500));
223 BOOST_TEST(res
== 3);
225 catch (std::exception
& ex
)
227 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
228 BOOST_TEST(false && "exception thrown");
232 BOOST_TEST(false && "exception thrown");
236 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
240 boost::shared_future
<int> f
= boost::async(f0
).share();
241 boost::this_thread::sleep_for(ms(300));
244 check_timer
timer(ms(500));
247 BOOST_TEST(res
== 3);
249 catch (std::exception
& ex
)
251 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
252 BOOST_TEST(false && "exception thrown");
256 BOOST_TEST(false && "exception thrown");
260 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
264 boost::future
<int> f
= boost::async(boost::launch::async
, f0
);
265 boost::this_thread::sleep_for(ms(300));
268 check_timer
timer(ms(500));
271 BOOST_TEST(res
== 3);
273 catch (std::exception
& ex
)
275 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
276 BOOST_TEST(false && "exception thrown");
280 BOOST_TEST(false && "exception thrown");
284 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
288 boost::future
<long> f
= boost::async(boost::launch::async
, A(3));
289 boost::this_thread::sleep_for(ms(300));
292 check_timer
timer(ms(500));
295 BOOST_TEST(res
== 3);
297 catch (std::exception
& ex
)
299 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
300 BOOST_TEST(false && "exception thrown");
304 BOOST_TEST(false && "exception thrown");
308 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
309 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
313 boost::future
<long> f
= boost::async(boost::launch::deferred
, A(3));
314 //boost::this_thread::sleep_for(ms(300));
317 check_timer
timer(ms(500));
320 BOOST_TEST(res
== 3);
322 catch (std::exception
& ex
)
324 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
325 BOOST_TEST(false && "exception thrown");
329 BOOST_TEST(false && "exception thrown");
334 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
335 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
340 boost::future
<long> f
= boost::async(boost::launch::async
, &A::doit
, &a
);
341 boost::this_thread::sleep_for(ms(300));
344 check_timer
timer(ms(500));
347 BOOST_TEST(res
== 3);
349 catch (std::exception
& ex
)
351 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
352 BOOST_TEST(false && "exception thrown");
356 BOOST_TEST(false && "exception thrown");
360 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
365 boost::future
<long> f
= boost::async(boost::launch::deferred
, &A::doit
, &a
);
366 boost::this_thread::sleep_for(ms(300));
369 check_timer
timer(ms(500));
372 BOOST_TEST(res
== 3);
374 catch (std::exception
& ex
)
376 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
377 BOOST_TEST(false && "exception thrown");
381 BOOST_TEST(false && "exception thrown");
386 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
390 boost::future
<int> f
= boost::async(boost::launch::async
, BOOST_THREAD_MAKE_RV_REF(MoveOnly()));
391 boost::this_thread::sleep_for(ms(300));
394 check_timer
timer(ms(500));
397 BOOST_TEST(res
== 3);
399 catch (std::exception
& ex
)
401 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
402 BOOST_TEST(false && "exception thrown");
406 BOOST_TEST(false && "exception thrown");
409 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
410 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
414 boost::future
<int> f
= boost::async(boost::launch::deferred
, BOOST_THREAD_MAKE_RV_REF(MoveOnly()));
415 boost::this_thread::sleep_for(ms(300));
418 check_timer
timer(ms(500));
421 BOOST_TEST(res
== 3);
423 catch (std::exception
& ex
)
425 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
426 BOOST_TEST(false && "exception thrown");
430 BOOST_TEST(false && "exception thrown");
434 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
438 boost::future
<int> f
= boost::async(boost::launch::any
, f0
);
439 boost::this_thread::sleep_for(ms(300));
442 check_timer
timer(ms(500));
445 BOOST_TEST(res
== 3);
447 catch (std::exception
& ex
)
449 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
450 BOOST_TEST(false && "exception thrown");
454 BOOST_TEST(false && "exception thrown");
457 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
458 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
462 boost::future
<int> f
= boost::async(boost::launch::deferred
, f0
);
463 //boost::this_thread::sleep_for(ms(300));
466 check_timer
timer(ms(500));
469 BOOST_TEST(res
== 3);
471 catch (std::exception
& ex
)
473 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
474 BOOST_TEST(false && "exception thrown");
478 BOOST_TEST(false && "exception thrown");
482 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
486 boost::future
<int&> f
= boost::async(f1
);
487 boost::this_thread::sleep_for(ms(300));
490 check_timer
timer(ms(500));
493 BOOST_TEST(res
== &i
);
495 catch (std::exception
& ex
)
497 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
498 BOOST_TEST(false && "exception thrown");
502 BOOST_TEST(false && "exception thrown");
505 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
509 boost::future
<int&> f
= boost::async(boost::launch::async
, f1
);
510 boost::this_thread::sleep_for(ms(300));
513 check_timer
timer(ms(500));
516 BOOST_TEST(res
== &i
);
518 catch (std::exception
& ex
)
520 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
521 BOOST_TEST(false && "exception thrown");
525 BOOST_TEST(false && "exception thrown");
528 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
532 boost::future
<int&> f
= boost::async(boost::launch::any
, f1
);
533 boost::this_thread::sleep_for(ms(300));
536 check_timer
timer(ms(500));
539 BOOST_TEST(res
== &i
);
541 catch (std::exception
& ex
)
543 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
544 BOOST_TEST(false && "exception thrown");
548 BOOST_TEST(false && "exception thrown");
551 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
552 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
556 boost::future
<int&> f
= boost::async(boost::launch::deferred
, f1
);
557 //boost::this_thread::sleep_for(ms(300));
560 check_timer
timer(ms(500));
563 BOOST_TEST(res
== &i
);
565 catch (std::exception
& ex
)
567 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
568 BOOST_TEST(false && "exception thrown");
572 BOOST_TEST(false && "exception thrown");
576 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
580 boost::future
<void> f
= boost::async(f2
);
581 boost::this_thread::sleep_for(ms(300));
583 check_timer
timer(ms(500));
587 catch (std::exception
& ex
)
589 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
590 BOOST_TEST(false && "exception thrown");
594 BOOST_TEST(false && "exception thrown");
597 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
601 boost::future
<void> f
= boost::async(boost::launch::async
, f2
);
602 boost::this_thread::sleep_for(ms(300));
604 check_timer
timer(ms(500));
608 catch (std::exception
& ex
)
610 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
611 BOOST_TEST(false && "exception thrown");
615 BOOST_TEST(false && "exception thrown");
618 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
622 boost::future
<void> f
= boost::async(boost::launch::any
, f2
);
623 boost::this_thread::sleep_for(ms(300));
625 check_timer
timer(ms(500));
629 catch (std::exception
& ex
)
631 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
632 BOOST_TEST(false && "exception thrown");
636 BOOST_TEST(false && "exception thrown");
639 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
640 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
644 boost::future
<void> f
= boost::async(boost::launch::deferred
, f2
);
645 //boost::this_thread::sleep_for(ms(300));
647 check_timer
timer(ms(500));
651 catch (std::exception
& ex
)
653 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
654 BOOST_TEST(false && "exception thrown");
658 BOOST_TEST(false && "exception thrown");
663 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
667 boost::future
<MoveOnly
> f
= boost::async(&f3_1
);
668 boost::this_thread::sleep_for(ms(300));
671 check_timer
timer(ms(500));
674 BOOST_TEST_EQ(res
.value
, 2);
676 catch (std::exception
& ex
)
678 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
679 BOOST_TEST(false && "exception thrown");
683 BOOST_TEST(false && "exception thrown");
686 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
687 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
691 boost::future
<MoveOnly
> f
= boost::async(boost::launch::deferred
, &f3_1
);
692 //boost::this_thread::sleep_for(ms(300));
695 check_timer
timer(ms(500));
698 BOOST_TEST_EQ(res
.value
, 2);
700 catch (std::exception
& ex
)
702 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
703 BOOST_TEST(false && "exception thrown");
707 BOOST_TEST(false && "exception thrown");
711 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
715 boost::future
<MoveOnly
> f
;
716 f
= boost::async(&f3_1
);
717 boost::this_thread::sleep_for(ms(300));
720 check_timer
timer(ms(500));
723 BOOST_TEST(res
.value
== 2);
725 catch (std::exception
& ex
)
727 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
728 BOOST_TEST(false && "exception thrown");
732 BOOST_TEST(false && "exception thrown");
735 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
739 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(&f3_0
);
740 boost::this_thread::sleep_for(ms(300));
741 boost::csbl::unique_ptr
<int> res
;
743 check_timer
timer(ms(500));
746 BOOST_TEST(*res
== 3);
748 catch (std::exception
& ex
)
750 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
751 BOOST_TEST(false && "exception thrown");
755 BOOST_TEST(false && "exception thrown");
759 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
760 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
764 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(boost::launch::async
, &f3
, 3);
765 boost::this_thread::sleep_for(ms(300));
766 boost::csbl::unique_ptr
<int> res
;
768 check_timer
timer(ms(500));
771 BOOST_TEST(*res
== 3);
773 catch (std::exception
& ex
)
775 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
776 BOOST_TEST(false && "exception thrown");
780 BOOST_TEST(false && "exception thrown");
783 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
787 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(boost::launch::deferred
, &f3
, 3);
788 //boost::this_thread::sleep_for(ms(300));
789 boost::csbl::unique_ptr
<int> res
;
791 check_timer
timer(ms(500));
794 BOOST_TEST(*res
== 3);
796 catch (std::exception
& ex
)
798 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
799 BOOST_TEST(false && "exception thrown");
803 BOOST_TEST(false && "exception thrown");
806 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
810 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(&f3
, 3);
811 boost::this_thread::sleep_for(ms(300));
812 boost::csbl::unique_ptr
<int> res
;
814 check_timer
timer(ms(500));
817 BOOST_TEST(*res
== 3);
819 catch (std::exception
& ex
)
821 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
822 BOOST_TEST(false && "exception thrown");
826 BOOST_TEST(false && "exception thrown");
831 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
832 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
836 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(boost::launch::async
, &f4
, boost::csbl::unique_ptr
<int>(new int(3)));
837 boost::this_thread::sleep_for(ms(300));
838 boost::csbl::unique_ptr
<int> res
;
840 check_timer
timer(ms(500));
843 BOOST_TEST(*res
== 3);
845 catch (std::exception
& ex
)
847 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
848 BOOST_TEST(false && "exception thrown");
852 BOOST_TEST(false && "exception thrown");
855 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
859 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(boost::launch::deferred
, &f4
, boost::csbl::unique_ptr
<int>(new int(3)));
860 //boost::this_thread::sleep_for(ms(300));
861 boost::csbl::unique_ptr
<int> res
;
863 check_timer
timer(ms(500));
866 BOOST_TEST(*res
== 3);
868 catch (std::exception
& ex
)
870 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
871 BOOST_TEST(false && "exception thrown");
875 BOOST_TEST(false && "exception thrown");
878 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<std::endl
;
882 boost::future
<boost::csbl::unique_ptr
<int> > f
= boost::async(&f4
, boost::csbl::unique_ptr
<int>(new int(3)));
883 boost::this_thread::sleep_for(ms(300));
884 boost::csbl::unique_ptr
<int> res
;
886 check_timer
timer(ms(500));
889 BOOST_TEST(*res
== 3);
891 catch (std::exception
& ex
)
893 std::cout
<< __FILE__
<<"["<<__LINE__
<<"]"<<ex
.what() << std::endl
;
894 BOOST_TEST(false && "exception thrown");
898 BOOST_TEST(false && "exception thrown");
902 return boost::report_errors();