Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 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 | |
| 21 | unsigned throw_one = 0xFFFF; |
| 22 | |
| 23 | void* 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 | |
| 31 | void operator delete(void* p) throw() |
| 32 | { |
| 33 | std::free(p); |
| 34 | } |
| 35 | |
| 36 | bool f_run = false; |
| 37 | |
| 38 | void f() |
| 39 | { |
| 40 | f_run = true; |
| 41 | } |
| 42 | |
| 43 | class G |
| 44 | { |
| 45 | int alive_; |
| 46 | public: |
| 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 | |
| 71 | int G::n_alive = 0; |
| 72 | bool G::op_run = false; |
| 73 | |
| 74 | int 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 Hinnant | 2cb7936 | 2010-08-22 00:50:25 +0000 | [diff] [blame] | 129 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | } |