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// friend void swap(jthread& x, jthread& y) 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
22template <class T>
23concept IsFreeSwapNoexcept = requires(T& a, T& b) {
24 { swap(a, b) } noexcept;
25};
26
27static_assert(IsFreeSwapNoexcept<std::jthread>);
28
29int main(int, char**) {
30 // x is default constructed
31 {
32 std::jthread t1;
33 std::jthread t2 = support::make_test_jthread([] {});
34 const auto originalId2 = t2.get_id();
35 swap(x&: t1, y&: t2);
36
37 assert(t1.get_id() == originalId2);
38 assert(t2.get_id() == std::jthread::id());
39 }
40
41 // y is default constructed
42 {
43 std::jthread t1 = support::make_test_jthread([] {});
44 std::jthread t2{};
45 const auto originalId1 = t1.get_id();
46 swap(x&: t1, y&: 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 swap(x&: t1, y&: 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 swap(x&: t1, y&: 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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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