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