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 join(); 16 17 #include <thread> 18 #include <new> 19 #include <cstdlib> 20 #include <cassert> 21 #include <system_error> 22 23 #include "make_test_thread.h" 24 #include "test_macros.h" 25 26 class G 27 { 28 int alive_; 29 public: 30 static int n_alive; 31 static bool op_run; 32 G()33 G() : alive_(1) {++n_alive;} G(const G & g)34 G(const G& g) : alive_(g.alive_) {++n_alive;} ~G()35 ~G() {alive_ = 0; --n_alive;} 36 operator ()()37 void operator()() 38 { 39 assert(alive_ == 1); 40 assert(n_alive >= 1); 41 op_run = true; 42 } 43 }; 44 45 int G::n_alive = 0; 46 bool G::op_run = false; 47 foo()48void foo() {} 49 main(int,char **)50int main(int, char**) 51 { 52 { 53 G g; 54 std::thread t0 = support::make_test_thread(g); 55 assert(t0.joinable()); 56 t0.join(); 57 assert(!t0.joinable()); 58 #ifndef TEST_HAS_NO_EXCEPTIONS 59 try { 60 t0.join(); 61 assert(false); 62 } catch (std::system_error const&) { 63 } 64 #endif 65 } 66 #ifndef TEST_HAS_NO_EXCEPTIONS 67 { 68 std::thread t0 = support::make_test_thread(foo); 69 t0.detach(); 70 try { 71 t0.join(); 72 assert(false); 73 } catch (std::system_error const&) { 74 } 75 } 76 #endif 77 78 return 0; 79 } 80