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// [[nodiscard]] bool joinable() const noexcept;
15
16#include <atomic>
17#include <cassert>
18#include <concepts>
19#include <thread>
20#include <type_traits>
21
22#include "make_test_thread.h"
23#include "test_macros.h"
24
25static_assert(noexcept(std::declval<const std::jthread&>().joinable()));
26
27int main(int, char**) {
28 // Default constructed
29 {
30 const std::jthread jt;
31 std::same_as<bool> decltype(auto) result = jt.joinable();
32 assert(!result);
33 }
34
35 // Non-default constructed
36 {
37 const std::jthread jt = support::make_test_jthread([] {});
38 std::same_as<bool> decltype(auto) result = jt.joinable();
39 assert(result);
40 }
41
42 // Non-default constructed
43 // the thread of execution has not finished
44 {
45 std::atomic_bool done = false;
46 const std::jthread jt = support::make_test_jthread([&done] { done.wait(false); });
47 std::same_as<bool> decltype(auto) result = jt.joinable();
48 done = true;
49 done.notify_all();
50 assert(result);
51 }
52
53 return 0;
54}
55

source code of libcxx/test/std/thread/thread.jthread/joinable.pass.cpp