[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / thread / thread.threads / thread.thread.class / thread.thread.member / detach.pass.cpp
bloba6b3a34d85706a710fca2267427e64a5494de3c5
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
11 // <thread>
13 // class thread
15 // void detach();
17 #include <thread>
18 #include <atomic>
19 #include <system_error>
20 #include <cassert>
22 #include "make_test_thread.h"
23 #include "test_macros.h"
25 std::atomic_bool done(false);
27 class G
29 int alive_;
30 bool done_;
31 public:
32 static int n_alive;
33 static bool op_run;
35 G() : alive_(1), done_(false)
37 ++n_alive;
40 G(const G& g) : alive_(g.alive_), done_(false)
42 ++n_alive;
44 ~G()
46 alive_ = 0;
47 --n_alive;
48 if (done_) done = true;
51 void operator()()
53 assert(alive_ == 1);
54 assert(n_alive >= 1);
55 op_run = true;
56 done_ = true;
60 int G::n_alive = 0;
61 bool G::op_run = false;
63 void foo() { done = true; }
65 int main(int, char**)
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);
77 assert(G::n_alive == 0);
78 done = false;
79 #ifndef TEST_HAS_NO_EXCEPTIONS
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&) {
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) {}
95 #endif
97 return 0;