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