xref: /llvm-project/libcxx/test/std/thread/thread.jthread/get_stop_token.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 // [[nodiscard]] stop_token get_stop_token() const noexcept;
14695138caSHui 
15695138caSHui #include <cassert>
16695138caSHui #include <concepts>
17695138caSHui #include <stop_token>
18695138caSHui #include <thread>
19695138caSHui #include <type_traits>
20695138caSHui #include <utility>
21695138caSHui 
22*475e1543SLouis Dionne #include "make_test_thread.h"
23695138caSHui #include "test_macros.h"
24695138caSHui 
25695138caSHui static_assert(noexcept(std::declval<const std::jthread&>().get_stop_token()));
26695138caSHui 
27695138caSHui int main(int, char**) {
28695138caSHui   // Represents a thread
29695138caSHui   {
30*475e1543SLouis Dionne     std::jthread jt                                 = support::make_test_jthread([] {});
31695138caSHui     auto ss                                         = jt.get_stop_source();
32695138caSHui     std::same_as<std::stop_token> decltype(auto) st = std::as_const(jt).get_stop_token();
33695138caSHui 
34695138caSHui     assert(st.stop_possible());
35695138caSHui     assert(!st.stop_requested());
36695138caSHui     ss.request_stop();
37695138caSHui     assert(st.stop_requested());
38695138caSHui   }
39695138caSHui 
40695138caSHui   // Does not represent a thread
41695138caSHui   {
42695138caSHui     const std::jthread jt{};
43695138caSHui     std::same_as<std::stop_token> decltype(auto) st = jt.get_stop_token();
44695138caSHui 
45695138caSHui     assert(!st.stop_possible());
46695138caSHui   }
47695138caSHui 
48695138caSHui   return 0;
49695138caSHui }
50