xref: /llvm-project/libcxx/test/std/thread/thread.jthread/join.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
1695138caSHui //===----------------------------------------------------------------------===//
2695138caSHui //
3695138caSHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4695138caSHui // See https://llvm.org/LICENSE.txt for license information.
5695138caSHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6695138caSHui //
7695138caSHui //===----------------------------------------------------------------------===//
8695138caSHui //
9695138caSHui // UNSUPPORTED: no-threads
10695138caSHui // UNSUPPORTED: c++03, c++11, c++14, c++17
11695138caSHui // XFAIL: availability-synchronization_library-missing
12695138caSHui 
13695138caSHui // void join();
14695138caSHui 
15695138caSHui #include <atomic>
16695138caSHui #include <cassert>
17695138caSHui #include <chrono>
18695138caSHui #include <concepts>
19695138caSHui #include <functional>
20695138caSHui #include <system_error>
21695138caSHui #include <thread>
22695138caSHui #include <type_traits>
23695138caSHui #include <vector>
24695138caSHui 
25*475e1543SLouis Dionne #include "make_test_thread.h"
26695138caSHui #include "test_macros.h"
27695138caSHui 
28695138caSHui int main(int, char**) {
29695138caSHui   // Effects: Blocks until the thread represented by *this has completed.
30695138caSHui   {
31695138caSHui     std::atomic_int calledTimes = 0;
32695138caSHui     std::vector<std::jthread> jts;
33695138caSHui     constexpr auto numberOfThreads = 10u;
34695138caSHui     jts.reserve(numberOfThreads);
35695138caSHui     for (auto i = 0u; i < numberOfThreads; ++i) {
36*475e1543SLouis Dionne       jts.emplace_back(support::make_test_jthread([&] {
37695138caSHui         std::this_thread::sleep_for(std::chrono::milliseconds(2));
38695138caSHui         calledTimes.fetch_add(1, std::memory_order_relaxed);
39*475e1543SLouis Dionne       }));
40695138caSHui     }
41695138caSHui 
42695138caSHui     for (auto i = 0u; i < numberOfThreads; ++i) {
43695138caSHui       jts[i].join();
44695138caSHui     }
45695138caSHui 
46695138caSHui     // If join did block, calledTimes must equal to numberOfThreads
47695138caSHui     // If join did not block, there is a chance that the check below happened
48695138caSHui     // before test threads incrementing the counter, thus calledTimed would
49695138caSHui     // be less than numberOfThreads.
50695138caSHui     // This is not going to catch issues 100%. Creating more threads to increase
51695138caSHui     // the probability of catching the issue
52695138caSHui     assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads);
53695138caSHui   }
54695138caSHui 
55695138caSHui   // Synchronization: The completion of the thread represented by *this synchronizes with
56695138caSHui   // ([intro.multithread]) the corresponding successful join() return.
57695138caSHui   {
58695138caSHui     bool flag       = false;
59*475e1543SLouis Dionne     std::jthread jt = support::make_test_jthread([&] { flag = true; });
60695138caSHui     jt.join();
61695138caSHui     assert(flag); // non atomic write is visible to the current thread
62695138caSHui   }
63695138caSHui 
64695138caSHui   // Postconditions: The thread represented by *this has completed. get_id() == id().
65695138caSHui   {
66*475e1543SLouis Dionne     std::jthread jt = support::make_test_jthread([] {});
67695138caSHui     assert(jt.get_id() != std::jthread::id());
68695138caSHui     jt.join();
69695138caSHui     assert(jt.get_id() == std::jthread::id());
70695138caSHui   }
71695138caSHui 
72695138caSHui #if !defined(TEST_HAS_NO_EXCEPTIONS)
73695138caSHui   // Throws: system_error when an exception is required ([thread.req.exception]).
74695138caSHui   // invalid_argument - if the thread is not joinable.
75695138caSHui   {
76695138caSHui     std::jthread jt;
77695138caSHui     try {
78695138caSHui       jt.join();
79695138caSHui       assert(false);
80695138caSHui     } catch (const std::system_error& err) {
81695138caSHui       assert(err.code() == std::errc::invalid_argument);
82695138caSHui     }
83695138caSHui   }
84695138caSHui 
85695138caSHui #endif
86695138caSHui 
87695138caSHui   return 0;
88695138caSHui }
89