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