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: c++03, c++11, c++14, c++17 |
11 | // XFAIL: availability-synchronization_library-missing |
12 | |
13 | // void swap(jthread& x) noexcept; |
14 | |
15 | #include <cassert> |
16 | #include <thread> |
17 | #include <type_traits> |
18 | |
19 | #include "make_test_thread.h" |
20 | #include "test_macros.h" |
21 | |
22 | template <class T> |
23 | concept IsMemberSwapNoexcept = requires(T& a, T& b) { |
24 | { a.swap(b) } noexcept; |
25 | }; |
26 | |
27 | static_assert(IsMemberSwapNoexcept<std::jthread>); |
28 | |
29 | int main(int, char**) { |
30 | // this is default constructed |
31 | { |
32 | std::jthread t1; |
33 | std::jthread t2 = support::make_test_jthread([] {}); |
34 | const auto originalId2 = t2.get_id(); |
35 | t1.swap(t&: t2); |
36 | |
37 | assert(t1.get_id() == originalId2); |
38 | assert(t2.get_id() == std::jthread::id()); |
39 | } |
40 | |
41 | // that is default constructed |
42 | { |
43 | std::jthread t1 = support::make_test_jthread([] {}); |
44 | std::jthread t2{}; |
45 | const auto originalId1 = t1.get_id(); |
46 | t1.swap(t&: t2); |
47 | |
48 | assert(t1.get_id() == std::jthread::id()); |
49 | assert(t2.get_id() == originalId1); |
50 | } |
51 | |
52 | // both not default constructed |
53 | { |
54 | std::jthread t1 = support::make_test_jthread([] {}); |
55 | std::jthread t2 = support::make_test_jthread([] {}); |
56 | const auto originalId1 = t1.get_id(); |
57 | const auto originalId2 = t2.get_id(); |
58 | t1.swap(t&: t2); |
59 | |
60 | assert(t1.get_id() == originalId2); |
61 | assert(t2.get_id() == originalId1); |
62 | } |
63 | |
64 | // both default constructed |
65 | { |
66 | std::jthread t1; |
67 | std::jthread t2; |
68 | t1.swap(t&: t2); |
69 | |
70 | assert(t1.get_id() == std::jthread::id()); |
71 | assert(t2.get_id() == std::jthread::id()); |
72 | } |
73 | |
74 | return 0; |
75 | } |
76 | |