Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / thread / test / sync / futures / promise / set_exception_at_thread_exit_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 // class promise<R>
18
19 // void promise::set_exception_at_thread_exit(exception_ptr p);
20
21 #define BOOST_THREAD_VERSION 4
22
23 #include <boost/thread/future.hpp>
24 #include <boost/detail/lightweight_test.hpp>
25 #include <boost/static_assert.hpp>
26
27 namespace boost
28 {
29   template <typename T>
30   struct wrap
31   {
32     wrap(T const& v) :
33       value(v)
34     {
35     }
36     T value;
37
38   };
39
40   template <typename T>
41   exception_ptr make_exception_ptr(T v)
42   {
43     return copy_exception(wrap<T> (v));
44   }
45 }
46
47 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
48 void func(boost::promise<int> p)
49 #else
50 boost::promise<int> p;
51 void func()
52 #endif
53 {
54   //p.set_exception(boost::make_exception_ptr(3));
55   p.set_exception_at_thread_exit(boost::make_exception_ptr(3));
56 }
57
58 int main()
59 {
60   {
61     typedef int T;
62 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
63     boost::promise<T> p;
64     boost::future<T> f = p.get_future();
65     boost::thread(func, boost::move(p)).detach();
66 #else
67     boost::future<T> f = p.get_future();
68     boost::thread(func).detach();
69 #endif
70     try
71     {
72       f.get();
73       BOOST_TEST(false);
74     }
75     catch (boost::wrap<int> i)
76     {
77       BOOST_TEST(i.value == 3);
78     }
79     catch (...)
80     {
81       BOOST_TEST(false);
82     }
83   }
84   {
85     typedef int T;
86     boost::promise<T> p2;
87     boost::future<T> f = p2.get_future();
88 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
89     boost::thread(func, boost::move(p2)).detach();
90 #else
91     p = boost::move(p2);
92     boost::thread(func).detach();
93 #endif
94     try
95     {
96       f.get();
97       BOOST_TEST(false);
98     }
99     catch (boost::wrap<int> i)
100     {
101       BOOST_TEST(i.value == 3);
102     }
103     catch (...)
104     {
105       BOOST_TEST(false);
106     }
107   }
108   return boost::report_errors();
109 }
110