1// Copyright (C) 2012-2013 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//#define BOOST_THREAD_USES_LOG
13#define BOOST_THREAD_USES_LOG_THREAD_ID
14
15#include <boost/thread/detail/log.hpp>
16#include <boost/thread/future.hpp>
17#include <boost/assert.hpp>
18#include <string>
19#include <iostream>
20
21#if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
22
23#ifdef BOOST_MSVC
24#pragma warning(disable: 4127) // conditional expression is constant
25#endif
26
27int p1()
28{
29 BOOST_THREAD_LOG << "P1" << BOOST_THREAD_END_LOG;
30 return 123;
31}
32
33boost::future<int> p2()
34{
35 BOOST_THREAD_LOG << "<P2" << BOOST_THREAD_END_LOG;
36 boost::future<int> f1 = boost::async(policy: boost::launch::async, f: &p1);
37 BOOST_THREAD_LOG << "P2>" << BOOST_THREAD_END_LOG;
38 return boost::move(t&: f1);
39}
40
41int main()
42{
43 const int number_of_tests = 100;
44 BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG;
45 for (int i=0; i< number_of_tests; i++)
46 try
47 {
48
49#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
50 {
51 boost::future<int> inner_future = boost::async(policy: boost::launch::async, f: &p2).unwrap();
52 inner_future.wait();
53 int ii = inner_future.get();
54 BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
55 }
56#endif
57 {
58 boost::future<boost::future<int> > outer_future = boost::async(policy: boost::launch::async, f: &p2);
59 boost::future<int> inner_future = outer_future.unwrap();
60 inner_future.wait();
61 int ii = inner_future.get();
62 BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
63 }
64 {
65 boost::future<boost::future<int> > outer_future = boost::async(policy: boost::launch::async, f: &p2);
66 boost::future<int> inner_future = outer_future.unwrap();
67 int ii = inner_future.get();
68 BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
69 }
70 }
71 catch (std::exception& ex)
72 {
73 std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl;
74 BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
75 return 1;
76 }
77 catch (...)
78 {
79 std::cout << " ERRORRRRR exception thrown" << std::endl;
80 BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
81 return 2;
82 }
83 BOOST_THREAD_LOG << "MAIN>" << BOOST_THREAD_END_LOG;
84 return 0;
85}
86#else
87
88int main()
89{
90 return 0;
91}
92#endif
93

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