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: no-threads 10 11 // <thread> 12 13 // class thread 14 15 // void join(); 16 17 #include <thread> 18 #include <new> 19 #include <cstdlib> 20 #include <cassert> 21 #include <system_error> 22 #include <atomic> 23 24 #include "make_test_thread.h" 25 #include "test_macros.h" 26 27 std::atomic_bool done(false); 28 29 class G 30 { 31 int alive_; 32 public: 33 static int n_alive; 34 static bool op_run; 35 G()36 G() : alive_(1) {++n_alive;} G(const G & g)37 G(const G& g) : alive_(g.alive_) {++n_alive;} ~G()38 ~G() {alive_ = 0; --n_alive;} 39 operator ()()40 void operator()() 41 { 42 assert(alive_ == 1); 43 assert(n_alive >= 1); 44 op_run = true; 45 } 46 }; 47 48 int G::n_alive = 0; 49 bool G::op_run = false; 50 foo()51void foo() { done = true; } 52 main(int,char **)53int main(int, char**) 54 { 55 { 56 G g; 57 std::thread t0 = support::make_test_thread(g); 58 assert(t0.joinable()); 59 t0.join(); 60 assert(!t0.joinable()); 61 #ifndef TEST_HAS_NO_EXCEPTIONS 62 try { 63 t0.join(); 64 assert(false); 65 } catch (std::system_error const&) { 66 } 67 #endif 68 } 69 #ifndef TEST_HAS_NO_EXCEPTIONS 70 { 71 std::thread t0 = support::make_test_thread(foo); 72 t0.detach(); 73 try { 74 t0.join(); 75 assert(false); 76 } catch (std::system_error const&) { 77 } 78 // Wait to make sure that the detached thread has started up. 79 // Without this, we could exit main and start destructing global 80 // resources that are needed when the thread starts up, while the 81 // detached thread would start up only later. 82 while (!done) {} 83 } 84 #endif 85 86 return 0; 87 } 88