1// Copyright (C) 2012 Vicente Botet
2//
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)
5
6#include <boost/config.hpp>
7#if ! defined BOOST_NO_CXX11_DECLTYPE
8#define BOOST_RESULT_OF_USE_DECLTYPE
9#endif
10
11#define BOOST_THREAD_VERSION 4
12
13#include <boost/thread/future.hpp>
14#include <iostream>
15
16namespace boost
17{
18
19 template <typename T>
20 exception_ptr make_exception_ptr(T v)
21 {
22 return copy_exception(v);
23 }
24}
25
26int p1() { return 5; }
27int& p1r() { static int i=0; return i; }
28
29void p() { }
30
31#if defined BOOST_THREAD_USES_MOVE
32boost::future<void> void_compute()
33{
34 return BOOST_THREAD_MAKE_RV_REF(boost::make_ready_future());
35}
36#endif
37
38boost::future<int> compute(int x)
39{
40 if (x == 0) return boost::make_ready_future(value: 0);
41#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
42 if (x < 0) return boost::make_exceptional_future<int>(std::logic_error("Error"));
43#else
44 if (x < 0) return boost::make_exceptional(ex: std::logic_error("Error"));
45#endif
46 //boost::future<int> f1 = boost::async([]() { return x+1; });
47 boost::future<int> f1 = boost::async(f: p1);
48 return boost::move(t&: f1);
49}
50
51boost::future<int&> compute_ref(int x)
52{
53 static int i = 0;
54 //if (x == 0) return boost::make_ready_future<int&>(i); //This must not compile as the type is deduced as boost::future<int>
55 if (x == 0) return boost::make_ready_no_decay_future<int&>(value: i);
56#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
57 if (x < 0) return boost::make_exceptional_future<int&>(std::logic_error("Error"));
58#else
59 if (x < 0) return boost::make_exceptional(ex: std::logic_error("Error"));
60#endif
61 boost::future<int&> f1 = boost::async(f: p1r);
62 return boost::move(t&: f1);
63}
64
65boost::shared_future<int> shared_compute(int x)
66{
67 if (x == 0) return boost::make_ready_future(value: 0).share();
68#ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
69 if (x < 0) return boost::make_exceptional_future<int>(std::logic_error("Error")).share();
70#else
71 if (x < 0) return boost::make_exceptional(ex: std::logic_error("Error"));
72#endif
73 //boost::future<int> f1 = boost::async([]() { return x+1; });
74 boost::shared_future<int> f1 = boost::async(f: &p1).share();
75 return f1;
76}
77
78
79int main()
80{
81 const int number_of_tests = 100;
82 for (int i=0; i< number_of_tests; i++)
83 try
84 {
85// {
86// std::cout << __FILE__ << " "<<__LINE__ << std::endl;
87// boost::future<int> f = boost::async(boost::launch::async, p1);
88// std::cout << i << " "<<f.get() << std::endl;
89// }
90#if defined BOOST_THREAD_USES_MOVE
91 {
92 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
93 boost::future<void> f = void_compute();
94 f.get();
95 }
96#endif
97 {
98 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
99 boost::future<int> f = compute(x: -1);
100 f.wait();
101 }
102 {
103 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
104 boost::future<int> f = compute(x: 0);
105 std::cout << f.get() << std::endl;
106 }
107 {
108 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
109 boost::future<int&> f = compute_ref(x: 0);
110 std::cout << f.get() << std::endl;
111 }
112#if __cplusplus > 201103L
113 {
114 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
115 int i = 0;
116 boost::future<int&> f = boost::make_ready_future(value: std::ref(t&: i));
117 std::cout << f.get() << std::endl;
118 }
119#endif
120 {
121 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
122 int i = 0;
123 boost::future<int&> f = boost::make_ready_future(value: boost::ref(t&: i));
124 std::cout << f.get() << std::endl;
125 }
126 {
127 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
128 const int i = 0;
129 boost::future<int const&> f = boost::make_ready_future(value: boost::cref(t: i));
130 std::cout << f.get() << std::endl;
131 }
132 {
133 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
134 boost::future<int> f = compute(x: 2);
135 std::cout << f.get() << std::endl;
136 }
137 {
138 std::cout << __FILE__ << " "<< __LINE__ << std::endl;
139 boost::shared_future<int> f = shared_compute(x: 0);
140 std::cout << f.get() << std::endl;
141 }
142 }
143 catch (std::exception& ex)
144 {
145 std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl;
146 return 1;
147 }
148 catch (...)
149 {
150 std::cout << "ERRORRRRR "<<"ERRORRRRR exception thrown" << std::endl;
151 return 2;
152 }
153 return 0;
154}
155

source code of boost/libs/thread/example/make_future.cpp