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 | // void join(); |
15 | |
16 | #include <atomic> |
17 | #include <cassert> |
18 | #include <chrono> |
19 | #include <concepts> |
20 | #include <functional> |
21 | #include <system_error> |
22 | #include <thread> |
23 | #include <type_traits> |
24 | #include <vector> |
25 | |
26 | #include "make_test_thread.h" |
27 | #include "test_macros.h" |
28 | |
29 | int main(int, char**) { |
30 | // Effects: Blocks until the thread represented by *this has completed. |
31 | { |
32 | std::atomic_int calledTimes = 0; |
33 | std::vector<std::jthread> jts; |
34 | constexpr auto numberOfThreads = 10u; |
35 | jts.reserve(numberOfThreads); |
36 | for (auto i = 0u; i < numberOfThreads; ++i) { |
37 | jts.emplace_back(support::make_test_jthread([&] { |
38 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
39 | calledTimes.fetch_add(1, std::memory_order_relaxed); |
40 | })); |
41 | } |
42 | |
43 | for (auto i = 0u; i < numberOfThreads; ++i) { |
44 | jts[i].join(); |
45 | } |
46 | |
47 | // If join did block, calledTimes must equal to numberOfThreads |
48 | // If join did not block, there is a chance that the check below happened |
49 | // before test threads incrementing the counter, thus calledTimed would |
50 | // be less than numberOfThreads. |
51 | // This is not going to catch issues 100%. Creating more threads to increase |
52 | // the probability of catching the issue |
53 | assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads); |
54 | } |
55 | |
56 | // Synchronization: The completion of the thread represented by *this synchronizes with |
57 | // ([intro.multithread]) the corresponding successful join() return. |
58 | { |
59 | bool flag = false; |
60 | std::jthread jt = support::make_test_jthread([&] { flag = true; }); |
61 | jt.join(); |
62 | assert(flag); // non atomic write is visible to the current thread |
63 | } |
64 | |
65 | // Postconditions: The thread represented by *this has completed. get_id() == id(). |
66 | { |
67 | std::jthread jt = support::make_test_jthread([] {}); |
68 | assert(jt.get_id() != std::jthread::id()); |
69 | jt.join(); |
70 | assert(jt.get_id() == std::jthread::id()); |
71 | } |
72 | |
73 | #if !defined(TEST_HAS_NO_EXCEPTIONS) |
74 | // Throws: system_error when an exception is required ([thread.req.exception]). |
75 | // invalid_argument - if the thread is not joinable. |
76 | { |
77 | std::jthread jt; |
78 | try { |
79 | jt.join(); |
80 | assert(false); |
81 | } catch (const std::system_error& err) { |
82 | assert(err.code() == std::errc::invalid_argument); |
83 | } |
84 | } |
85 | |
86 | #endif |
87 | |
88 | return 0; |
89 | } |
90 | |