Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 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 | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 12 | // NOTE: TSAN will report this test as leaking a thread. |
| 13 | // XFAIL: tsan |
Eric Fiselier | 6be02cb | 2015-04-02 21:12:17 +0000 | [diff] [blame] | 14 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 15 | // <thread> |
| 16 | |
| 17 | // class thread |
| 18 | |
| 19 | // ~thread(); |
| 20 | |
| 21 | #include <thread> |
| 22 | #include <new> |
| 23 | #include <cstdlib> |
| 24 | #include <cassert> |
| 25 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | class G |
| 27 | { |
| 28 | int alive_; |
| 29 | public: |
| 30 | static int n_alive; |
| 31 | static bool op_run; |
| 32 | |
| 33 | G() : alive_(1) {++n_alive;} |
| 34 | G(const G& g) : alive_(g.alive_) {++n_alive;} |
| 35 | ~G() {alive_ = 0; --n_alive;} |
| 36 | |
| 37 | void operator()() |
| 38 | { |
| 39 | assert(alive_ == 1); |
Marshall Clow | c3a9b81 | 2013-03-26 15:28:33 +0000 | [diff] [blame] | 40 | assert(n_alive >= 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 41 | op_run = true; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | int G::n_alive = 0; |
| 46 | bool G::op_run = false; |
| 47 | |
| 48 | void f1() |
| 49 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 50 | std::exit(0); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int main() |
| 54 | { |
| 55 | std::set_terminate(f1); |
| 56 | { |
| 57 | assert(G::n_alive == 0); |
| 58 | assert(!G::op_run); |
Eric Fiselier | 6be02cb | 2015-04-02 21:12:17 +0000 | [diff] [blame] | 59 | G g; |
| 60 | { |
| 61 | std::thread t(g); |
| 62 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
| 63 | } |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | } |
| 65 | assert(false); |
| 66 | } |