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: c++03, c++11, c++14, c++17
11 // XFAIL: availability-synchronization_library-missing
20 #include <type_traits>
23 #include "make_test_thread.h"
24 #include "test_macros.h"
26 int main(int, char**) {
30 assert(!jt
.joinable());
33 // If joinable() is true, calls request_stop() and then join().
34 // request_stop is called
36 std::optional
<std::jthread
> jt
= support::make_test_jthread([] {});
38 std::stop_callback
cb(jt
->get_stop_token(), [&called
] { called
= true; });
43 // If joinable() is true, calls request_stop() and then join().
46 std::atomic_int calledTimes
= 0;
47 std::vector
<std::jthread
> jts
;
49 constexpr auto numberOfThreads
= 10u;
50 jts
.reserve(numberOfThreads
);
51 for (auto i
= 0u; i
< numberOfThreads
; ++i
) {
52 jts
.emplace_back(support::make_test_jthread([&calledTimes
] {
53 std::this_thread::sleep_for(std::chrono::milliseconds
{2});
54 calledTimes
.fetch_add(1, std::memory_order_relaxed
);
59 // If join was called as expected, calledTimes must equal to numberOfThreads
60 // If join was not called, there is a chance that the check below happened
61 // before test threads incrementing the counter, thus calledTimed would
62 // be less than numberOfThreads.
63 // This is not going to catch issues 100%. Creating more threads would increase
64 // the probability of catching the issue
65 assert(calledTimes
.load(std::memory_order_relaxed
) == numberOfThreads
);