线程对象本身是不可以拷贝的,只能move。看下面的程序:
void some_function() { } void some_other_function() { } void call_by_main() { std::thread t1(some_function); std::thread t2 = std::move(t1); t1 = std::thread(some_other_function);//临时对象(未具名对象)可以直接转化为右值引用 std::thread t3; t3 = std::move(t2); //此时t1对应some_other_function,t3对应some_function,t2已经没有关联的线程 t1 = std::move(t3);//错误:t1本来关联了一个线程,导致std::terminate()被调用 }
一个没有关联线程的thread对象,可以通过move或者直接赋值为另一个未具名thread对象的方式来再次关联一个线程。
在函数中返回thread对象,可以直接返回未具名对象,也可以返回具名临时对象,原理也是move而不是copy。看下面的示例:
void f1() { cout << __FUNCTION__ << endl; } void f2() { cout << __FUNCTION__ << endl; } thread g1() { return thread(f1); } thread g2() { thread t(f2); return t; } void call_by_main() { thread t1 = g1(); t1.join(); thread t2 = g2(); t2.join(); }
thread对象作为函数参数,看下面示例:void f() { cout << __FUNCTION__ << endl; } void g(thread t) { t.join(); } void call_by_main() { //方式1: thread t1(f); g(std::move(t1)); //方式2: g(thread(f)); }