blob: f41a70b9cfc2896f46c8109ebb2e9f6e4cd5c5b5 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <thread>
11
12// class thread
13
14// template <class F, class ...Args> thread(F&& f, Args&&... args);
15
16#include <thread>
17#include <new>
18#include <cstdlib>
19#include <cassert>
20
21unsigned throw_one = 0xFFFF;
22
23void* operator new(std::size_t s) throw(std::bad_alloc)
24{
25 if (throw_one == 0)
26 throw std::bad_alloc();
27 --throw_one;
28 return std::malloc(s);
29}
30
31void operator delete(void* p) throw()
32{
33 std::free(p);
34}
35
36bool f_run = false;
37
38void f()
39{
40 f_run = true;
41}
42
43class G
44{
45 int alive_;
46public:
47 static int n_alive;
48 static bool op_run;
49
50 G() : alive_(1) {++n_alive;}
51 G(const G& g) : alive_(g.alive_) {++n_alive;}
52 ~G() {alive_ = 0; --n_alive;}
53
54 void operator()()
55 {
56 assert(alive_ == 1);
57 assert(n_alive >= 1);
58 op_run = true;
59 }
60
61 void operator()(int i, double j)
62 {
63 assert(alive_ == 1);
64 assert(n_alive >= 1);
65 assert(i == 5);
66 assert(j == 5.5);
67 op_run = true;
68 }
69};
70
71int G::n_alive = 0;
72bool G::op_run = false;
73
74int main()
75{
76 {
77 std::thread t(f);
78 t.join();
79 assert(f_run == true);
80 }
81 f_run = false;
82 {
83 try
84 {
85 throw_one = 0;
86 std::thread t(f);
87 assert(false);
88 }
89 catch (...)
90 {
91 throw_one = 0xFFFF;
92 assert(!f_run);
93 }
94 }
95 {
96 assert(G::n_alive == 0);
97 assert(!G::op_run);
98 std::thread t((G()));
99 t.join();
100 assert(G::n_alive == 0);
101 assert(G::op_run);
102 }
103 G::op_run = false;
104 {
105 try
106 {
107 throw_one = 0;
108 assert(G::n_alive == 0);
109 assert(!G::op_run);
110 std::thread t((G()));
111 assert(false);
112 }
113 catch (...)
114 {
115 throw_one = 0xFFFF;
116 assert(G::n_alive == 0);
117 assert(!G::op_run);
118 }
119 }
120#ifndef _LIBCPP_HAS_NO_VARIADICS
121 {
122 assert(G::n_alive == 0);
123 assert(!G::op_run);
124 std::thread t(G(), 5, 5.5);
125 t.join();
126 assert(G::n_alive == 0);
127 assert(G::op_run);
128 }
Howard Hinnant2cb79362010-08-22 00:50:25 +0000129#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant3e519522010-05-11 19:42:16 +0000130}