1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: libcpp-has-no-threads 10 11 // <thread> 12 13 // class thread 14 15 // void detach(); 16 17 #include <thread> 18 #include <atomic> 19 #include <system_error> 20 #include <cassert> 21 22 #include "make_test_thread.h" 23 #include "test_macros.h" 24 25 std::atomic_bool done(false); 26 27 class G 28 { 29 int alive_; 30 bool done_; 31 public: 32 static int n_alive; 33 static bool op_run; 34 G()35 G() : alive_(1), done_(false) 36 { 37 ++n_alive; 38 } 39 G(const G & g)40 G(const G& g) : alive_(g.alive_), done_(false) 41 { 42 ++n_alive; 43 } ~G()44 ~G() 45 { 46 alive_ = 0; 47 --n_alive; 48 if (done_) done = true; 49 } 50 operator ()()51 void operator()() 52 { 53 assert(alive_ == 1); 54 assert(n_alive >= 1); 55 op_run = true; 56 done_ = true; 57 } 58 }; 59 60 int G::n_alive = 0; 61 bool G::op_run = false; 62 foo()63void foo() {} 64 main(int,char **)65int main(int, char**) 66 { 67 { 68 G g; 69 std::thread t0 = support::make_test_thread(g); 70 assert(t0.joinable()); 71 t0.detach(); 72 assert(!t0.joinable()); 73 while (!done) {} 74 assert(G::op_run); 75 assert(G::n_alive == 1); 76 } 77 assert(G::n_alive == 0); 78 #ifndef TEST_HAS_NO_EXCEPTIONS 79 { 80 std::thread t0 = support::make_test_thread(foo); 81 assert(t0.joinable()); 82 t0.detach(); 83 assert(!t0.joinable()); 84 try { 85 t0.detach(); 86 } catch (std::system_error const&) { 87 } 88 } 89 #endif 90 91 return 0; 92 } 93