]>
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 | // Copyright (C) 2011 Vicente J. Botet Escriba | |
10 | // | |
11 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
12 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
13 | ||
14 | // <boost/thread/thread.hpp> | |
15 | ||
16 | // class thread | |
17 | ||
18 | // template <class F, class ...Args> thread(F f, Args... args); | |
19 | ||
20 | #include <new> | |
21 | #include <cstdlib> | |
22 | #include <cassert> | |
23 | #include <boost/thread/thread_only.hpp> | |
24 | #include <boost/detail/lightweight_test.hpp> | |
25 | ||
26 | unsigned throw_one = 0xFFFF; | |
27 | ||
28 | #if defined _GLIBCXX_THROW | |
29 | void* operator new(std::size_t s) _GLIBCXX_THROW (std::bad_alloc) | |
30 | #elif defined BOOST_MSVC | |
31 | void* operator new(std::size_t s) | |
32 | #else | |
33 | void* operator new(std::size_t s) throw (std::bad_alloc) | |
34 | #endif | |
35 | { | |
36 | //std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
37 | if (throw_one == 0) throw std::bad_alloc(); | |
38 | --throw_one; | |
39 | return std::malloc(s); | |
40 | } | |
41 | ||
42 | #if defined BOOST_MSVC | |
43 | void operator delete(void* p) | |
44 | #else | |
45 | void operator delete(void* p) throw () | |
46 | #endif | |
47 | { | |
48 | //std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
49 | std::free(p); | |
50 | } | |
51 | ||
52 | bool f_run = false; | |
53 | ||
54 | void f() | |
55 | { | |
56 | f_run = true; | |
57 | } | |
58 | ||
59 | class G | |
60 | { | |
61 | int alive_; | |
62 | public: | |
63 | static int n_alive; | |
64 | static bool op_run; | |
65 | ||
66 | G() : | |
67 | alive_(1) | |
68 | { | |
69 | ++n_alive; | |
70 | } | |
71 | G(const G& g) : | |
72 | alive_(g.alive_) | |
73 | { | |
74 | ++n_alive; | |
75 | } | |
76 | ~G() | |
77 | { | |
78 | alive_ = 0; | |
79 | --n_alive; | |
80 | } | |
81 | ||
82 | void operator()() | |
83 | { | |
84 | BOOST_TEST(alive_ == 1); | |
85 | //BOOST_TEST(n_alive >= 1); | |
86 | op_run = true; | |
87 | } | |
88 | ||
89 | }; | |
90 | ||
91 | int G::n_alive = 0; | |
92 | bool G::op_run = false; | |
93 | ||
94 | ||
95 | int main() | |
96 | { | |
97 | { | |
98 | boost::thread t(f); | |
99 | t.join(); | |
100 | BOOST_TEST(f_run == true); | |
101 | } | |
102 | f_run = false; | |
103 | #ifndef BOOST_MSVC | |
104 | { | |
105 | try | |
106 | { | |
107 | throw_one = 0; | |
108 | boost::thread t(f); | |
109 | BOOST_TEST(false); | |
110 | } | |
111 | catch (...) | |
112 | { | |
113 | throw_one = 0xFFFF; | |
114 | BOOST_TEST(!f_run); | |
115 | } | |
116 | } | |
117 | #endif | |
118 | { | |
119 | BOOST_TEST(G::n_alive == 0); | |
120 | BOOST_TEST(!G::op_run); | |
121 | boost::thread t( (G())); | |
122 | t.join(); | |
123 | BOOST_TEST(G::n_alive == 0); | |
124 | BOOST_TEST(G::op_run); | |
125 | } | |
126 | #ifndef BOOST_MSVC | |
127 | G::op_run = false; | |
128 | { | |
129 | try | |
130 | { | |
131 | throw_one = 0; | |
132 | BOOST_TEST(G::n_alive == 0); | |
133 | BOOST_TEST(!G::op_run); | |
134 | boost::thread t( (G())); | |
135 | BOOST_TEST(false); | |
136 | } | |
137 | catch (...) | |
138 | { | |
139 | throw_one = 0xFFFF; | |
140 | BOOST_TEST(G::n_alive == 0); | |
141 | std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl; | |
142 | BOOST_TEST(!G::op_run); | |
143 | } | |
144 | } | |
145 | #endif | |
146 | ||
147 | return boost::report_errors(); | |
148 | } |