9.future其他成员函数,shared_future,atomic

1.future其他成员函数

std::future_status::timeout

等待1秒,因为线程入口函数要5秒,所以超时了(没有等到)

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread() {//线程入口函数
	cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

	std::chrono::microseconds dura(5000);//sleep 5s
	std::this_thread::sleep_for(dura);//休息一定时长

	cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
	return 5;
}

int main()
{

	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到

	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(mythread);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	
	//返回枚举类型
	//wait_for等待一段时间
	std::future_status status = result.wait_for(std::chrono::seconds(1));//等待1秒
	if (status == std::future_status::timeout) {//超时:我想等待你1秒,希望你返回,你没有返回,那么status == timeout
		//超时,表示线程还没执行完
		cout << "超时,线程还没执行完毕" << endl;

	}
	else if (status == std::future_status::ready) {//成功执行完毕
		//表示线程成功执行完毕
		cout << "线程执行完毕,返回" << endl;
		cout << result.get() << endl;
	}
	else if (status == std::future_status::deferred) {//延迟
		//如果async的第一个参数被设置为std::launch::deferred,则本条件成立
		cout << "线程被延迟执行" << endl;
		//到get这里才开始执行
		cout << result.get() << endl;
	}
	//cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}
/*
output:

	main thread id = 10012
	continue.....
	mythread() start, thread id = 4860
	超时,线程还没执行完毕
	hello world
	mythread() end, thread id = 4860


*/




std::future_status::ready

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread() {//线程入口函数
	cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

	std::chrono::microseconds dura(5000);//sleep 5s
	std::this_thread::sleep_for(dura);//休息一定时长

	cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
	return 5;
}

int main()
{

	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到

	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(mythread);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	
	//返回枚举类型
	//wait_for等待一段时间
	std::future_status status = result.wait_for(std::chrono::seconds(6));//等待6秒
	if (status == std::future_status::timeout) {//超时:我想等待你6秒,希望你返回,你没有返回,那么status == timeout
		//超时,表示线程还没执行完
		cout << "超时,线程还没执行完毕" << endl;

	}
	else if (status == std::future_status::ready) {//成功执行完毕
		//表示线程成功执行完毕
		cout << "线程执行完毕,返回" << endl;
		cout << result.get() << endl;
	}
	else if (status == std::future_status::deferred) {//延迟
		//如果async的第一个参数被设置为std::launch::deferred,则本条件成立
		cout << "线程被延迟执行" << endl;
		//到get这里才开始执行
		cout << result.get() << endl;
	}
	//cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}
/*

output:
	main thread id = 6776
	continue.....
	mythread() start, thread id = 2360
	mythread() end, thread id = 2360
	线程执行完毕,返回
	5
	hello world

*/




std::future_status::deferred

如果async的第一个参数被设置为std::launch::deferred,则wait_for返回的是std::future_status::deferred

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread() {//线程入口函数
	cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

	std::chrono::microseconds dura(5000);//sleep 5s
	std::this_thread::sleep_for(dura);//休息一定时长

	cout << "mythread() end, thread id = " << std::this_thread::get_id() << endl;
	return 5;
}

int main()
{

	//一:std::async, std::future创建后台任务并返回值
	/*
		std::async是个函数模板,用来启动一个异步任务,启动一个异步任务之后,他返回一个std::future对象,std::future是个类模板
		什么叫“启动一个异步任务”:就是自动创建一个线程并开始执行对应的线程入口函数,他返回一个std::future对象,
			这个std::future对象里边就含有线程入口函数所返回的结果(线程返回的结果),我们可以通过调用future对象的成员函数get()来获取结果;
			future:将来的意思,有人也称呼std::future提供了一种访问异步操作结果的机制,就是说这个结果你可能不能马上得到,
			在线程执行完毕的时候,你就能拿到结果了。所以大家就这么理解:这个future(对象)里会保存一个值,在将来的某个时刻可以拿到

	*/
	//下列程序通过std::future对象的get()成员函数等待线程执行结束并返回结果
	//这个get()函数很牛,不拿到将来的返回值誓不罢休,不拿到值我就卡在这等待拿值
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::future<int>result = std::async(std::launch::deferred, mythread);//创建一个线程并开始执行,绑定关系
	cout << "continue....." << endl;
	
	//返回枚举类型
	//wait_for等待一段时间
	std::future_status status = result.wait_for(std::chrono::seconds(1));//等待1秒
	if (status == std::future_status::timeout) {//超时:我想等待你1秒,希望你返回,你没有返回,那么status == timeout
		//超时,表示线程还没执行完
		cout << "超时,线程还没执行完毕" << endl;

	}
	else if (status == std::future_status::ready) {//成功执行完毕
		//表示线程成功执行完毕
		cout << "线程执行完毕,返回" << endl;
		cout << result.get() << endl;
	}
	else if (status == std::future_status::deferred) {//延迟
		//如果async的第一个参数被设置为std::launch::deferred,则本条件成立
		cout << "线程被延迟执行" << endl;
		//到get这里才开始执行
		cout << result.get() << endl;
	}
	//cout << result.get() << endl;//卡在这里等待mythread()执行完毕,拿到结果,get()只能调用一次
	//result.wait();//等待线程返回,本身并不返回结果
	cout << "hello world" << endl;


	return 0;
}
/*
output:
没有创建子线程
	main thread id = 7920
	continue.....
	线程被延迟执行
	mythread() start, thread id = 7920
	mythread() end, thread id = 7920
	5
	hello world

*/




2.std::shared_future

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mythread(int mypar) {//线程入口函数

	cout << mypar << endl;
	cout << "mythread() start, thread id = " << std::this_thread::get_id() << endl;

	std::chrono::microseconds dura(5000);//sleep 5s
	std::this_thread::sleep_for(dura);//休息一定时长

	return 5;
}
void mythread2(std::shared_future<int>& tmpf) {
	cout << "mthread2() start" << " threadid = " << std::this_thread::get_id() << endl;
	auto result = tmpf.get();//get值可以获取多次了
	result = tmpf.get();
	result = tmpf.get();
	result = tmpf.get();
	result = tmpf.get();
	cout << "mythread2 result = " << result << endl;
}
int main()
{
	/*

		因为future类成员函数get只能调用一次,万一多个线程函数都想得到get的值呢? ==>  引出std::shared_future
		二.std::shared_future是个类模板
			get()函数复制数据

	*/
	cout << "main thread id = " << std::this_thread::get_id() << endl;
	std::packaged_task<int(int)>mypt(mythread);//我们把函数mythread通过packaged_task包装起来
	std::thread t1(std::ref(mypt), 1);//线程直接开始执行。第二个参数作为线程入口函数的参数。
	t1.join();//等待线程执行完毕

	std::future<int>result = mypt.get_future();//std::future对象里包含有线程入口函数的返回结果,这里result保存mythread返回的结果
	bool flag = result.valid();//true  是有效值
	std::shared_future<int>result_s(std::move(result));//执行完后result_s里有值了,而result空了
	flag = result.valid();//false  是无效值
	std::thread t2(mythread2, std::ref(result_s));
	t2.join();

	cout << "hello world" << endl;
	return 0;
}


/*
output:
	main thread id = 15268
	1
	mythread() start, thread id = 1868
	mthread2() start threadid = 16076
	mythread2 result = 5
	hello world


*/


3.原子操作std::atomic

3.1原子操作概念引出范例

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mycount = 0;
void mythread() {
	for (int i = 0; i < 1000000; i++) {
		mycount++;
	}
}
int main()
{
	/*

		二.std::atomic
			互斥量:多线程编程中 保护共享数据:
			有两个线程,对一个变量进行操作,这个线程读,另一个线程往这个变量中写值 

	*/
	std::thread t1(mythread);
	std::thread t2(mythread);

	t1.join();
	t2.join();
	cout << "两个线程执行完毕,最终mycount的值 = " << mycount << endl;
	
	return 0;
}


/*
output:
	两个线程执行完毕,最终mycount的值 = 1076772
*/


结果并不是200万!

3.1.1加上mutex

正确执行了,但是效率很慢

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
using namespace std;

int mycount = 0;
std::mutex mymutex;
void mythread() {
	for (int i = 0; i < 1000000; i++) {
		mymutex.lock();//粒度小
		//unique_lock<mutex>my(mymutex);
		mycount++;
		mymutex.unlock();
	}
}
int main()
{
	/*

		二.std::atomic
			互斥量:多线程编程中 保护共享数据:
			有两个线程,对一个变量进行操作,这个线程读,另一个线程往这个变量中写值 

	*/

	std::thread t1(mythread);
	std::thread t2(mythread);

	t1.join();
	t2.join();
	cout << "两个线程执行完毕,最终mycount的值 = " << mycount << endl;

	
	return 0;
}
/*
output:
	两个线程执行完毕,最终mycount的值 = 2000000
*/


3.2 原子操作

1.大家可以把原子操作理解成一种:不需要用到互斥锁(无锁)技术的多线程并发编程方式
2.或者:原子操作是在多线程中不会被打断的程序执行片段
3.原子操作比互斥量效率高!
4.互斥量加锁一般是针对一个代码段,而原子操作针对一个变量,要么是完成的,要么是没完成的,不可能出现半完成状态

eg1

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
#include<atomic>
using namespace std;


std::atomic<int> mycount{ 0 };//封装了一个类型为int的对象(值)

void mythread() {
	for (int i = 0; i < 1000000; i++) {
		mycount++;	//原子操作不会被打断
	}
}
int main()
{
	/*

		二.std::atomic
			互斥量:多线程编程中 保护共享数据:
			有两个线程,对一个变量进行操作,这个线程读,另一个线程往这个变量中写值 
			
	*/
	int a;

	std::thread t1(mythread);
	std::thread t2(mythread);

	t1.join();
	t2.join();
	cout << "两个线程执行完毕,最终mycount的值 = " << mycount << endl;

	
	return 0;
}


/*
output:
	两个线程执行完毕,最终mycount的值 = 2000000
*/


eg2

#include <iostream>
#include <vector>
#include <ctime>
#include<thread>
#include<mutex>
#include<future>
#include<atomic>
using namespace std;


std::atomic<bool> isend{ false };//线程退出标记,这里是原子操作,防止读和写乱套

void mythread() {
	std::chrono::milliseconds dura(1000);
	while (!isend) {
		//系统没要求线程退出,所以本线程干自己想干的事
		cout << "thread id = " << std::this_thread::get_id() << "运行中" << endl;
		std::this_thread::sleep_for(dura);//sleep 1s
	}
	cout << "thread id = " << std::this_thread::get_id() << "运行结束..." << endl;
}
int main()
{
	/*

		二.std::atomic
			互斥量:多线程编程中 保护共享数据:
			有两个线程,对一个变量进行操作,这个线程读,另一个线程往这个变量中写值 
			
	*/
	int a;

	std::thread t1(mythread);
	std::thread t2(mythread);

	std::chrono::milliseconds dura(5000);
	std::this_thread::sleep_for(dura);
	isend = true;//对原子对象的写操作,让线程自行运行结束
	t1.join();
	t2.join();
	cout << "两个线程执行完毕 "  << endl;

	
	return 0;
}


/*
output:
	thread id = 9732运行中
	thread id = 14392运行中
	thread id = 14392运行中
	thread id = 9732运行中
	thread id = 14392运行中
	thread id = 9732运行中
	thread id = 14392运行中
	thread id = 9732运行中
	thread id = 14392运行中
	thread id = 9732运行中
	thread id = 14392运行结束...
	thread id = 9732运行结束...
	两个线程执行完毕
*/


3.3 心得

一般用于计数或者统计(累计发送出去了多少个数据包,累计接收到了多少个数据包)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值