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
21 #include <type_traits>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 int main(int, char**) {
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([] {});
39 std::stop_callback
cb(jt
->get_stop_token(), [&called
] { called
= true; });
44 // If joinable() is true, calls request_stop() and then join().
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
);
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
);