c++多线程小练习

#include <iostream>
#include <thread>

using namespace std;

void func(string &s)
{
	cout<<"hello,this is my thread,thread id is"
	<<this_thread::get_id()<<endl;
	
}


int main()
{
	string s = "test";
	thread th = thread(func,std::ref(s));		//以引用的方式传递参数
	//th.detach();  //将线程剥离出主线程,失去对其控制
	th.join();
	cout<<"this is main thread"<<endl;
	
}

/********************************************************/
//function object
struct A{
	void operator() (){
		cout<<"I'm A"<<endl;
	}
}

int main()
{
	thread th = thread(A());
	th.join();
}

/******************************************************/
//lambda在多线程中的使用
//c++ lambda表达式详解:https://www.cnblogs.com/jimodetiantang/p/9016826.html
 int main()
 {
	 string s = "test";
	 //[]为外部变量参数列表,()为参数列表
	 auto f = [&s](int a,int b){
		 cout<<s<<endl;
		 cout<< a+b<<endl;
	 };
	 //多线程的方式
	 thread f1 = thread([&s](int a,int b){
		 cout<<z<<" "<<a+b<<endl;
	 },2,3);
	 f1.join();
	 f(1,2);
	 
 }

/******************************************************/
//多线程时间效率的测量
//C++类模板 template <class T>:https://www.cnblogs.com/msymm/p/9750787.html
template<class T>
void measure(T&& func)
{
	using namespace std::chrono;
	auto start = sysytem_clock::now();
	func();
	duration<double> diff_ = sysytem_clock::now() - start;
	
	cout<<diff_.count()<<"seconds"<<endl;
}


int main()
{

	measure([](){
		long long s =0;
		for(int i = 0; i<1000;i++)
			s+=i;
		cout<<s<<endl;
	});

}





/********************************************************/
//多线程加速小测验
template <class T>
void measure(T&& func)
{
	using namespace std::chrono;
	auto start = system_clock::now();
	func();
	duration<double> diff = sysytem_clock::now() - start;
	cout<<"cost:"<<diff.count()<<" seconds"<<endl;
}

void sum(long start,long end,long& ans)
{
	long s = 0;
	for(long i = start; i<end;i++)
	{
		s+=i;
	}
	ans = s;
}

long s = 1000000000;
int main()
{
	measure([](){
		long ans1,ans2;
		thread t1 = thread(sum,0,s/2,std::ref(ans1));
		thread t2 = thread(sum,s/2,s,std::ref(ans2));
		t1.join();
		t2.join();
		cout<<ans1+ans2<<endl;
	});
	
	
	measure([](){
		long ans;
		sum(0,s,ans);
		cout<<ans<<endl;
	});
	
}




/****************************************************/
//使用异步接口std::async和std::feature实现多线程
//参考博客:https://www.cnblogs.com/qicosmos/p/3534211.html
#include <feature>

 
template <class T>
void measure(T&& func)
{
	using namespace std::chrono;
	auto start = system_clock::now();
	func();
	duration<double> diff = sysytem_clock::now() - start;
	cout<<"cost:"<<diff.count()<<" seconds"<<endl;
}

long sum(long start,long end)
{
	long s = 0;
	for(long i = start; i<end;i++)
	{
		s+=i;
	}
	return s;
}

const long s = 1000000;
int main()
{
	measure([](){
		const int k =4;
		vector<future<long>> vf;
		vf.reverse(k);
		
		for(int i = 0; i<k;i++)
		{
			vf.push_back(std::async(sum, i == 0 ? 0 : (s/k)*i,(s/k)*(i+1)))
		}
		
		long ans=0;
		for(int i =0;i<k;i++)
			ans+=vf[i].get();
		cout<<ans<<endl;
	});
	
	
}



/***************************************************/
//c++中的互斥锁
std::mutex mtx;

void sum(int &s)
{
	mtx.lock();
	for(long i = 0; i<1000;i++)
	{
		s++;
	}
	mtx.unlock();
}

const long s = 1000000;
int main()
{
	measure([](){
		const int k =4;
		int s{0};
		vector<thread> vf;
		
		for(int i = 0; i<k;i++)
		{
			vf.emplace_back(sum, std::ref(s));
		}
		for(int j =0;j<4;j++)
		{
			vf[j].join();
		}
		
		cout<<s<<endl;
	});
	
	
}






















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值