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 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 
12 // <thread>
13 
14 // class thread
15 
16 // void detach();
17 
18 #include <thread>
19 #include <cassert>
20 
21 #include "test_atomic.h"
22 
23 AtomicBool done(false);
24 
25 class G
26 {
27     int alive_;
28     bool done_;
29 public:
30     static int n_alive;
31     static bool op_run;
32 
33     G() : alive_(1), done_(false)
34     {
35         ++n_alive;
36     }
37 
38     G(const G& g) : alive_(g.alive_), done_(false)
39     {
40         ++n_alive;
41     }
42     ~G()
43     {
44         alive_ = 0;
45         --n_alive;
46         if (done_) done = true;
47     }
48 
49     void operator()()
50     {
51         assert(alive_ == 1);
52         assert(n_alive >= 1);
53         op_run = true;
54         done_ = true;
55     }
56 };
57 
58 int G::n_alive = 0;
59 bool G::op_run = false;
60 
61 int main()
62 {
63     {
64         G g;
65         std::thread t0(g);
66         assert(t0.joinable());
67         t0.detach();
68         assert(!t0.joinable());
69         while (!done) {}
70         assert(G::op_run);
71         assert(G::n_alive == 1);
72     }
73     assert(G::n_alive == 0);
74 }
75