1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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;
22 #include <type_traits>
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([] {});
36 auto ssource
= j
.get_stop_source();
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([] {});
47 std::stop_callback
cb(j1
.get_stop_token(), [&called
] { called
= true; });
49 std::jthread j2
= support::make_test_jthread([] {});
54 // if joinable() is true, calls request_stop() and then join()
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
);
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();
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([] {});
100 assert(j2
.get_id() == std::jthread::id());
101 assert(!j2
.get_stop_source().stop_possible());
107 std::jthread j2
= support::make_test_jthread([] {});
109 auto j2Id
= j2
.get_id();
113 assert(j1
.get_id() == j2Id
);