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 | |
14 | // ~jthread(); |
15 | |
16 | #include <atomic> |
17 | #include <cassert> |
18 | #include <optional> |
19 | #include <stop_token> |
20 | #include <thread> |
21 | #include <type_traits> |
22 | #include <vector> |
23 | |
24 | #include "make_test_thread.h" |
25 | #include "test_macros.h" |
26 | |
27 | int main(int, char**) { |
28 | // !joinable() |
29 | { |
30 | std::jthread jt; |
31 | assert(!jt.joinable()); |
32 | } |
33 | |
34 | // If joinable() is true, calls request_stop() and then join(). |
35 | // request_stop is called |
36 | { |
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); |
42 | } |
43 | |
44 | // If joinable() is true, calls request_stop() and then join(). |
45 | // join is called |
46 | { |
47 | std::atomic_int calledTimes = 0; |
48 | std::vector<std::jthread> jts; |
49 | |
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 | })); |
57 | } |
58 | jts.clear(); |
59 | |
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); |
67 | } |
68 | |
69 | return 0; |
70 | } |
71 | |