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