使用swap进行两个线程的交换
#include<thread> #include<iostream> using namespace std; //交换线程 int main1() { thread t1([](){cout << "fangfang"<<endl;}); thread t2([](){cout << "huahua"<<endl;}); cout << "t1.get_id()" << t1.get_id() << "t2.get_id()" << t2.get_id() << endl; swap(t1, t2); cout << "t1.get_id()" << t1.get_id() << "t2.get_id()" << t2.get_id() << endl; t1.join(); t2.join(); }