a41aea56b6d46d10cfec55d4e2fde8e2bc6ef152
[platform/upstream/boost.git] / libs / thread / test / sync / futures / async / async_pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // Copyright (C) 2011 Vicente J. Botet Escriba
11 //
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)
14
15 // <boost/thread/future.hpp>
16
17 // template <class F, class... Args>
18 //     future<typename result_of<F(Args...)>::type>
19 //     async(F&& f, Args&&... args);
20
21 // template <class F, class... Args>
22 //     future<typename result_of<F(Args...)>::type>
23 //     async(launch policy, F&& f, Args&&... args);
24
25
26 #include <boost/thread/future.hpp>
27 #include <boost/thread/thread.hpp>
28 #include <boost/interprocess/smart_ptr/unique_ptr.hpp>
29 #include <memory>
30 #include <boost/detail/lightweight_test.hpp>
31
32 typedef boost::chrono::high_resolution_clock Clock;
33 typedef boost::chrono::milliseconds ms;
34
35 int f0()
36 {
37   boost::this_thread::sleep_for(ms(200));
38   return 3;
39 }
40
41 int i = 0;
42
43 int& f1()
44 {
45   boost::this_thread::sleep_for(ms(200));
46   return i;
47 }
48
49 void f2()
50 {
51   boost::this_thread::sleep_for(ms(200));
52 }
53
54 boost::interprocess::unique_ptr<int> f3(int i)
55 {
56   boost::this_thread::sleep_for(ms(200));
57   return boost::interprocess::unique_ptr<int>(new int(i));
58 }
59
60 boost::interprocess::unique_ptr<int> f4(boost::interprocess::unique_ptr<int>&& p)
61 {
62   boost::this_thread::sleep_for(ms(200));
63   return boost::move(p);
64 }
65
66 int main()
67 {
68   {
69     boost::future<int> f = boost::async(f0);
70     boost::this_thread::sleep_for(ms(300));
71     Clock::time_point t0 = Clock::now();
72     BOOST_TEST(f.get() == 3);
73     Clock::time_point t1 = Clock::now();
74     BOOST_TEST(t1 - t0 < ms(100));
75   }
76   {
77     boost::future<int> f = boost::async(boost::launch::async, f0);
78     boost::this_thread::sleep_for(ms(300));
79     Clock::time_point t0 = Clock::now();
80     BOOST_TEST(f.get() == 3);
81     Clock::time_point t1 = Clock::now();
82     BOOST_TEST(t1 - t0 < ms(100));
83   }
84   {
85     boost::future<int> f = boost::async(boost::launch::any, f0);
86     boost::this_thread::sleep_for(ms(300));
87     Clock::time_point t0 = Clock::now();
88     BOOST_TEST(f.get() == 3);
89     Clock::time_point t1 = Clock::now();
90     BOOST_TEST(t1 - t0 < ms(100));
91   }
92   {
93     boost::future<int> f = boost::async(boost::launch::deferred, f0);
94     boost::this_thread::sleep_for(ms(300));
95     Clock::time_point t0 = Clock::now();
96     BOOST_TEST(f.get() == 3);
97     Clock::time_point t1 = Clock::now();
98     BOOST_TEST(t1 - t0 > ms(100));
99   }
100
101   {
102     boost::future<int&> f = boost::async(f1);
103     boost::this_thread::sleep_for(ms(300));
104     Clock::time_point t0 = Clock::now();
105     BOOST_TEST(&f.get() == &i);
106     Clock::time_point t1 = Clock::now();
107     BOOST_TEST(t1 - t0 < ms(100));
108   }
109   {
110     boost::future<int&> f = boost::async(boost::launch::async, f1);
111     boost::this_thread::sleep_for(ms(300));
112     Clock::time_point t0 = Clock::now();
113     BOOST_TEST(&f.get() == &i);
114     Clock::time_point t1 = Clock::now();
115     BOOST_TEST(t1 - t0 < ms(100));
116   }
117   {
118     boost::future<int&> f = boost::async(boost::launch::any, f1);
119     boost::this_thread::sleep_for(ms(300));
120     Clock::time_point t0 = Clock::now();
121     BOOST_TEST(&f.get() == &i);
122     Clock::time_point t1 = Clock::now();
123     BOOST_TEST(t1 - t0 < ms(100));
124   }
125   {
126     boost::future<int&> f = boost::async(boost::launch::deferred, f1);
127     boost::this_thread::sleep_for(ms(300));
128     Clock::time_point t0 = Clock::now();
129     BOOST_TEST(&f.get() == &i);
130     Clock::time_point t1 = Clock::now();
131     BOOST_TEST(t1 - t0 > ms(100));
132   }
133
134   {
135     boost::future<void> f = boost::async(f2);
136     boost::this_thread::sleep_for(ms(300));
137     Clock::time_point t0 = Clock::now();
138     f.get();
139     Clock::time_point t1 = Clock::now();
140     BOOST_TEST(t1 - t0 < ms(100));
141   }
142   {
143     boost::future<void> f = boost::async(boost::launch::async, f2);
144     boost::this_thread::sleep_for(ms(300));
145     Clock::time_point t0 = Clock::now();
146     f.get();
147     Clock::time_point t1 = Clock::now();
148     BOOST_TEST(t1 - t0 < ms(100));
149   }
150   {
151     boost::future<void> f = boost::async(boost::launch::any, f2);
152     boost::this_thread::sleep_for(ms(300));
153     Clock::time_point t0 = Clock::now();
154     f.get();
155     Clock::time_point t1 = Clock::now();
156     BOOST_TEST(t1 - t0 < ms(100));
157   }
158   {
159     boost::future<void> f = boost::async(boost::launch::deferred, f2);
160     boost::this_thread::sleep_for(ms(300));
161     Clock::time_point t0 = Clock::now();
162     f.get();
163     Clock::time_point t1 = Clock::now();
164     BOOST_TEST(t1 - t0 > ms(100));
165   }
166
167   {
168     boost::future<boost::interprocess::unique_ptr<int>> f = boost::async(f3, 3);
169     boost::this_thread::sleep_for(ms(300));
170     Clock::time_point t0 = Clock::now();
171     BOOST_TEST(*f.get() == 3);
172     Clock::time_point t1 = Clock::now();
173     BOOST_TEST(t1 - t0 < ms(100));
174   }
175
176   {
177     boost::future<boost::interprocess::unique_ptr<int>> f = boost::async(f4, boost::interprocess::unique_ptr<int>(new int(3)));
178     boost::this_thread::sleep_for(ms(300));
179     Clock::time_point t0 = Clock::now();
180     BOOST_TEST(*f.get() == 3);
181     Clock::time_point t1 = Clock::now();
182     BOOST_TEST(t1 - t0 < ms(100));
183   }
184   return boost::report_errors();
185 }
186