10.std::atomic续谈,std::async深入谈

1.原子操作std::atomic续谈

一般原子操作争对++,–,+=,&=,|=,^=是支持的,其他的可能不支持

#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++;	//原子操作不会被打断
		//mycount += 1; //原子操作不会被打断
		mycount = mycount + 1;//结果不详
	}
}
int main()
{
	/*

		1.原子操作std::atomic续谈
			一般原子操作争对++,--,+=,&=,|=,^=是支持的,其他的可能不支持
	*/
	

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

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


	return 0;
}


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




2.std::async深入谈

2.1 std::async参数详述

/*

	老师讲解过一个延迟调用的参数叫std::launch::deferred[延迟调用],以及std::launch::async[强制创建一个线程]
	std::thread()如果系统资源紧张,那么可能创建线程就会失败,那么执行std::thread()时整个程序可能崩溃
	std::async()我们一般不叫创建线程(解释async),我们一般叫它创建一个异步任务
	std::async和std::最明显的不同,就是async有时候不创建新线程
	a)如果你用std::launch::deferred来调用async会怎么样?
		deferred延迟调用,并且不创建新线程,延迟到future对象调用get()或者wait()的时候才执行mythread()
		如果没有调用get()或者wait(),那么这个mythread()不会执行
	b)std::launch::async:强制这个异步任务在新线程执行,这意味着系统必须要创建新线程来执行mythread()
	c)std::launch::async|std::launch::deferred
		这个|:意味着两种方式都可能
	d)我们不带额外参数只给async函数一个入口函数名
		默认值是std::launch::async|std::launch::deferred,系统会自己决定异步(创建新线程)还是同步(不创建)方式运行

		

*/

2.2 std::async和std::thread的区别

a)std::thread()如果系统资源紧张,那么可能创建线程就会失败,那么执行std::thread()时整个程序可能崩溃
		int mythread(){return 1;}
		thread t1(mythread);
		t1.join();
		std::thraed创建线程的方式,如果线程返回值,你想拿到这个值不容易;
b)std::async()我们一般不叫创建线程(解释async),我们一般叫它创建一个异步任务(可能创建新线程),并且较容易拿到返回值
		std::async()如果遇到系统资源紧张,且没有传固定调用方式参数的情况下(默认std::launch::async|std::launch::deferred),不会创建新线程
		而是运行在get()语句所在的线程上.
		经验:一个程序里线程数量不宜超过100-200,时间片

2.3 std::async不确定性问题的解决

问题焦点在于std::future<int>result = std::async(mythread);写法
这个异步任务到底有没有被推迟执行(std::launch::async|std::launch::deferred)
std::future对象的wait_for函数,上节课讲过
#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::future对象的get()成员函数等待线程执行结束并返回结果
	
	std::future<int>result = std::async(mythread);


	//返回枚举类型
	//wait_for等待一段时间
	std::future_status status = result.wait_for(std::chrono::seconds(0));//等待0秒
	if (status == std::future_status::deferred) {//延迟
		//系统资源紧张了,给我采用std::launch::deferred策略了
		cout << "线程被延迟执行" << endl;
		//到get这里才开始执行
		cout << result.get() << endl;
	}
	else if (status == std::future_status::timeout) {//超时:我想等待你0秒,希望你返回,你没有返回,那么status == timeout
		//超时,表示线程还没执行完
		cout << "超时,线程还没执行完毕" << endl;

	}
	else if (status == std::future_status::ready) {//成功执行完毕
		//表示线程成功执行完毕
		cout << "线程执行完毕,返回" << endl;
		cout << result.get() << endl;
	}

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值