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]] stop_token get_stop_token() const noexcept;
15
16#include <cassert>
17#include <concepts>
18#include <stop_token>
19#include <thread>
20#include <type_traits>
21#include <utility>
22
23#include "make_test_thread.h"
24#include "test_macros.h"
25
26static_assert(noexcept(std::declval<const std::jthread&>().get_stop_token()));
27
28int main(int, char**) {
29 // Represents a thread
30 {
31 std::jthread jt = support::make_test_jthread([] {});
32 auto ss = jt.get_stop_source();
33 std::same_as<std::stop_token> decltype(auto) st = std::as_const(jt).get_stop_token();
34
35 assert(st.stop_possible());
36 assert(!st.stop_requested());
37 ss.request_stop();
38 assert(st.stop_requested());
39 }
40
41 // Does not represent a thread
42 {
43 const std::jthread jt{};
44 std::same_as<std::stop_token> decltype(auto) st = jt.get_stop_token();
45
46 assert(!st.stop_possible());
47 }
48
49 return 0;
50}
51

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