blob: ddf96d09573062dd6da1740c7973c4adbc11940d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011
Dan Albert1d4a1ed2016-05-25 22:36:09 -070012// NOTE: TSAN will report this test as leaking a thread.
13// XFAIL: tsan
Eric Fiselier6be02cb2015-04-02 21:12:17 +000014
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015// <thread>
16
17// class thread
18
19// ~thread();
20
21#include <thread>
22#include <new>
23#include <cstdlib>
24#include <cassert>
25
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026class G
27{
28 int alive_;
29public:
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 Clowc3a9b812013-03-26 15:28:33 +000040 assert(n_alive >= 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041 op_run = true;
42 }
43};
44
45int G::n_alive = 0;
46bool G::op_run = false;
47
48void f1()
49{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070050 std::exit(0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051}
52
53int main()
54{
55 std::set_terminate(f1);
56 {
57 assert(G::n_alive == 0);
58 assert(!G::op_run);
Eric Fiselier6be02cb2015-04-02 21:12:17 +000059 G g;
60 {
61 std::thread t(g);
62 std::this_thread::sleep_for(std::chrono::milliseconds(250));
63 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064 }
65 assert(false);
66}