Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.jthread / dtor.pass.cpp
blob35be0f6c0dd82c8e95914b48d1df86c1891feb99
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 // UNSUPPORTED: libcpp-has-no-experimental-stop_token
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // XFAIL: availability-synchronization_library-missing
14 // ~jthread();
16 #include <atomic>
17 #include <cassert>
18 #include <optional>
19 #include <stop_token>
20 #include <thread>
21 #include <type_traits>
22 #include <vector>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 int main(int, char**) {
28 // !joinable()
30 std::jthread jt;
31 assert(!jt.joinable());
34 // If joinable() is true, calls request_stop() and then join().
35 // request_stop is called
37 std::optional<std::jthread> jt = support::make_test_jthread([] {});
38 bool called = false;
39 std::stop_callback cb(jt->get_stop_token(), [&called] { called = true; });
40 jt.reset();
41 assert(called);
44 // If joinable() is true, calls request_stop() and then join().
45 // join is called
47 std::atomic_int calledTimes = 0;
48 std::vector<std::jthread> jts;
50 constexpr auto numberOfThreads = 10u;
51 jts.reserve(numberOfThreads);
52 for (auto i = 0u; i < numberOfThreads; ++i) {
53 jts.emplace_back(support::make_test_jthread([&calledTimes] {
54 std::this_thread::sleep_for(std::chrono::milliseconds{2});
55 calledTimes.fetch_add(1, std::memory_order_relaxed);
56 }));
58 jts.clear();
60 // If join was called as expected, calledTimes must equal to numberOfThreads
61 // If join was not called, there is a chance that the check below happened
62 // before test threads incrementing the counter, thus calledTimed would
63 // be less than numberOfThreads.
64 // This is not going to catch issues 100%. Creating more threads would increase
65 // the probability of catching the issue
66 assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads);
69 return 0;