Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.jthread / assign.move.pass.cpp
blob89521ad7660a120beda46c00f69741df5ca39e94
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
13 // ADDITIONAL_COMPILE_FLAGS: -Wno-self-move
15 // jthread& operator=(jthread&&) noexcept;
17 #include <atomic>
18 #include <cassert>
19 #include <concepts>
20 #include <stop_token>
21 #include <thread>
22 #include <type_traits>
23 #include <utility>
24 #include <vector>
26 #include "make_test_thread.h"
27 #include "test_macros.h"
29 static_assert(std::is_nothrow_move_assignable_v<std::jthread>);
31 int main(int, char**) {
32 // If &x == this is true, there are no effects.
34 std::jthread j = support::make_test_jthread([] {});
35 auto id = j.get_id();
36 auto ssource = j.get_stop_source();
37 j = std::move(j);
38 assert(j.get_id() == id);
39 assert(j.get_stop_source() == ssource);
42 // if joinable() is true, calls request_stop() and then join()
43 // request_stop is called
45 std::jthread j1 = support::make_test_jthread([] {});
46 bool called = false;
47 std::stop_callback cb(j1.get_stop_token(), [&called] { called = true; });
49 std::jthread j2 = support::make_test_jthread([] {});
50 j1 = std::move(j2);
51 assert(called);
54 // if joinable() is true, calls request_stop() and then join()
55 // join is called
57 std::atomic_int calledTimes = 0;
58 std::vector<std::jthread> jts;
59 constexpr auto numberOfThreads = 10u;
60 jts.reserve(numberOfThreads);
61 for (auto i = 0u; i < numberOfThreads; ++i) {
62 jts.emplace_back(support::make_test_jthread([&] {
63 std::this_thread::sleep_for(std::chrono::milliseconds(2));
64 calledTimes.fetch_add(1, std::memory_order_relaxed);
65 }));
68 for (auto i = 0u; i < numberOfThreads; ++i) {
69 jts[i] = std::jthread{};
72 // If join was called as expected, calledTimes must equal to numberOfThreads
73 // If join was not called, there is a chance that the check below happened
74 // before test threads incrementing the counter, thus calledTimed would
75 // be less than numberOfThreads.
76 // This is not going to catch issues 100%. Creating more threads to increase
77 // the probability of catching the issue
78 assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads);
81 // then assigns the state of x to *this
83 std::jthread j1 = support::make_test_jthread([] {});
84 std::jthread j2 = support::make_test_jthread([] {});
85 auto id2 = j2.get_id();
86 auto ssource2 = j2.get_stop_source();
88 j1 = std::move(j2);
90 assert(j1.get_id() == id2);
91 assert(j1.get_stop_source() == ssource2);
94 // sets x to a default constructed state
96 std::jthread j1 = support::make_test_jthread([] {});
97 std::jthread j2 = support::make_test_jthread([] {});
98 j1 = std::move(j2);
100 assert(j2.get_id() == std::jthread::id());
101 assert(!j2.get_stop_source().stop_possible());
104 // joinable is false
106 std::jthread j1;
107 std::jthread j2 = support::make_test_jthread([] {});
109 auto j2Id = j2.get_id();
111 j1 = std::move(j2);
113 assert(j1.get_id() == j2Id);
116 return 0;