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: libcpp-has-no-threads 10 11 // <thread> 12 13 // class thread 14 15 // void swap(thread& x, thread& y); 16 17 #include <thread> 18 #include <new> 19 #include <cstdlib> 20 #include <cassert> 21 22 class G 23 { 24 int alive_; 25 public: 26 static int n_alive; 27 static bool op_run; 28 29 G() : alive_(1) {++n_alive;} 30 G(const G& g) : alive_(g.alive_) {++n_alive;} 31 ~G() {alive_ = 0; --n_alive;} 32 33 void operator()() 34 { 35 assert(alive_ == 1); 36 assert(n_alive >= 1); 37 op_run = true; 38 } 39 }; 40 41 int G::n_alive = 0; 42 bool G::op_run = false; 43 44 int main(int, char**) 45 { 46 { 47 G g; 48 std::thread t0(g); 49 std::thread::id id0 = t0.get_id(); 50 std::thread t1; 51 std::thread::id id1 = t1.get_id(); 52 swap(t0, t1); 53 assert(t0.get_id() == id1); 54 assert(t1.get_id() == id0); 55 t1.join(); 56 } 57 58 return 0; 59 } 60