]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) 2013 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 | // class shared_future<R> | |
18 | ||
19 | // template <class Rep, class Period> | |
20 | // void wait() const; | |
21 | ||
22 | //#define BOOST_THREAD_VERSION 3 | |
23 | #define BOOST_THREAD_VERSION 4 | |
24 | //#define BOOST_THREAD_USES_LOG | |
25 | #define BOOST_THREAD_USES_LOG_THREAD_ID | |
26 | #include <boost/thread/detail/log.hpp> | |
27 | ||
28 | #include <boost/thread/future.hpp> | |
29 | #include <boost/thread/thread.hpp> | |
30 | #include <boost/chrono/chrono_io.hpp> | |
31 | #include <boost/detail/lightweight_test.hpp> | |
32 | ||
33 | #if defined BOOST_THREAD_USES_CHRONO | |
34 | ||
b32b8144 FG |
35 | #ifdef BOOST_MSVC |
36 | #pragma warning(disable: 4127) // conditional expression is constant | |
37 | #endif | |
38 | ||
7c673cae FG |
39 | typedef boost::chrono::milliseconds ms; |
40 | ||
41 | namespace boost | |
42 | { | |
43 | template <typename OStream> | |
44 | OStream& operator<<(OStream& os , boost::future_status st ) | |
45 | { | |
46 | os << underlying_cast<int>(st) << " "; | |
47 | return os; | |
48 | } | |
7c673cae FG |
49 | } |
50 | ||
51 | void func1(boost::promise<int> p) | |
52 | { | |
53 | boost::this_thread::sleep_for(ms(500)); | |
54 | p.set_value(3); | |
55 | } | |
56 | ||
57 | int j = 0; | |
58 | ||
59 | void func3(boost::promise<int&> p) | |
60 | { | |
61 | boost::this_thread::sleep_for(ms(500)); | |
62 | j = 5; | |
63 | p.set_value(j); | |
64 | } | |
65 | ||
66 | void func5(boost::promise<void> p) | |
67 | { | |
68 | boost::this_thread::sleep_for(ms(500)); | |
69 | p.set_value(); | |
70 | } | |
71 | ||
72 | int main() | |
73 | { | |
74 | BOOST_THREAD_LOG << BOOST_THREAD_END_LOG; | |
75 | { | |
76 | typedef boost::chrono::high_resolution_clock Clock; | |
77 | { | |
78 | typedef int T; | |
79 | boost::promise<T> p; | |
80 | boost::shared_future<T> f((p.get_future())); | |
81 | #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) | |
82 | boost::thread(func1, boost::move(p)).detach(); | |
83 | #else | |
84 | func1(boost::move(p)); | |
85 | #endif | |
86 | BOOST_TEST(f.valid()); | |
87 | f.wait(); | |
88 | BOOST_TEST(f.valid()); | |
89 | Clock::time_point t0 = Clock::now(); | |
90 | f.wait(); | |
91 | Clock::time_point t1 = Clock::now(); | |
92 | BOOST_TEST(f.valid()); | |
93 | BOOST_TEST(t1 - t0 < ms(50)); | |
94 | } | |
95 | { | |
96 | typedef int& T; | |
97 | boost::promise<T> p; | |
98 | boost::shared_future<T> f((p.get_future())); | |
99 | #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) | |
100 | boost::thread(func3, boost::move(p)).detach(); | |
101 | #else | |
102 | func3(boost::move(p)); | |
103 | #endif | |
104 | BOOST_TEST(f.valid()); | |
105 | f.wait(); | |
106 | BOOST_TEST(f.valid()); | |
107 | Clock::time_point t0 = Clock::now(); | |
108 | f.wait(); | |
109 | Clock::time_point t1 = Clock::now(); | |
110 | BOOST_TEST(f.valid()); | |
111 | BOOST_TEST(t1 - t0 < ms(50)); | |
112 | } | |
113 | { | |
114 | typedef void T; | |
115 | boost::promise<T> p; | |
116 | boost::shared_future<T> f((p.get_future())); | |
117 | #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) | |
118 | boost::thread(func5, boost::move(p)).detach(); | |
119 | #else | |
120 | func5(boost::move(p)); | |
121 | #endif | |
122 | BOOST_TEST(f.valid()); | |
123 | f.wait(); | |
124 | BOOST_TEST(f.valid()); | |
125 | Clock::time_point t0 = Clock::now(); | |
126 | f.wait(); | |
127 | Clock::time_point t1 = Clock::now(); | |
128 | BOOST_TEST(f.valid()); | |
129 | BOOST_TEST(t1 - t0 < ms(50)); | |
130 | } | |
131 | } | |
132 | BOOST_THREAD_LOG << BOOST_THREAD_END_LOG; | |
133 | ||
134 | return boost::report_errors(); | |
135 | } | |
136 | ||
137 | #else | |
138 | #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" | |
139 | #endif |