《C++ Concurrency in Action》笔记3 move线程对象

线程对象本身是不可以拷贝的,只能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));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值