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