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 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()63 void foo() { done = true; }
64 
main(int,char **)65 int 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     done = false;
79 #ifndef TEST_HAS_NO_EXCEPTIONS
80     {
81         std::thread t0 = support::make_test_thread(foo);
82         assert(t0.joinable());
83         t0.detach();
84         assert(!t0.joinable());
85         try {
86             t0.detach();
87         } catch (std::system_error const&) {
88         }
89         // Wait to make sure that the detached thread has started up.
90         // Without this, we could exit main and start destructing global
91         // resources that are needed when the thread starts up, while the
92         // detached thread would start up only later.
93         while (!done) {}
94     }
95 #endif
96 
97   return 0;
98 }
99