p11-12,,,,

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
void f1(int n)
{
	for (int i = 0;i<5;++i){
		std::cout<<"Thread 1 executing\n";
		++n;
		std::this_thread::sleep_for(std::chrono::milliseconds(10));		
	}
}
void f2(int&n)
{
	for(int i=0;i<5;++i){
		std::cout<<"Thread 2 executing\n";
		++n;
		std::this_thread::sleep_for(std::chrono::milliseconds(10));
	}
}
class foo
{
	public:
		void bar()
		{
			for(int i=0;i<5;++i){
				std::cout<<"Thread 3 executing\n";
				++n;
				std::this_thread::sleep_for(std::chrono::milliseconds(10));
			}
		}
int n=0;
};
class baz
{
	public:
		void operator()()
		{
			for(int i=0;i<5;++i){
				std::cout<<"Thread 4 executing\n";
				++n;
				std::this_thread::sleep_for(std::chrono::milliseconds(10));
				
			}
		}
		int n=0;
};
int main()
{
	int n=0;
	foo f;
	baz b;
	std::thread t1;
	std::thread t2(f1,n+1);
	std::thread t3(f2,std::ref(n));
	std::thread t4(std::move(t3));
	std::thread t5(&foo::bar,&f);
	std::thread t6(b);
	t2.join();
	t4.join();
	t5.join();
	t6.join();
	std::cout<<"Final value of n is"<<n<<'\n';
	std::cout<<"Final value of foo::n is"<<f.n<<'\n';
	
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值