11.atomic续谈、async深入谈

目录

在这里插入图片描述

一、原子操作std::atomic续谈

#include <iostream>
#include <thread>
#include <atomic>
using namespace std;
std::atomic<int> g_count = 0; //封装了一个类型为int的 对象(值)
 
void mythread1() {
	for (int i = 0; i < 1000000; i++) {
		 //虽然g_count使用了原子操作模板,但是这种写法既读又写,
		 //会导致计数错误
             g_count = g_count + 1;
	}
}

int main() {
	std::thread t1(mythread1);
	std::thread t2(mythread1);
	t1.join();
	t2.join();
	cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

在这里插入图片描述

一般atomic原子操作,针对++,–,+=,-=,&=,|=,^=是支持的,其他操作不一定支持。

二、std::async深入谈

2.1 std::async参数详述

async 用来创建一个异步任务

1、延迟调用参数 std::launch::deferred【延迟调用】,std::launch::async【强制创建一个线程】

2、std::async()我们一般不叫创建线程(他能够创建线程),我们一般叫它创建一个异步任务。

3、所以async和thread有什么区别呢?

std::asyncstd::thread最明显的不同,就是 async 有时候并不创建新线程。

① 如果用std::launch::deferred 来调用async

​ 延迟到调用 get() 或者 wait() 时执行,***如果不调用就不会执行。***

② 如果用std::launch::async来调用async

​ 强制这个异步任务在新线程上执行,这意味着,***系统必须要创建出新线程来运行入口函数。***

③ 如果同时用 std::launch::async | std::launch::deferred呢?

​ 如果同时用 std::launch::async | std::launch::deferred,这里这个或者关系意味着async的行为可能是 std::launch::async 创建新线程立即执行, 也可能是 std::launch::deferred 没有创建新线程并且延迟到调用get()执行,由系统根据实际情况来决定采取哪种方案。

④ 如果不带额外参数,直接给一个入口函数名呢?

​ 不带额外参数 std::async(mythread),只给async 一个入口函数名,此时的系统给的默认值是 std::launch::async | std::launch::deferred 和 ③ 一样,由系统自行决定异步还是同步运行。

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

1、std::thread()如果系统资源紧张可能出现创建线程失败的情况,如果创建线程失败那么程序就可能崩溃==,而且不容易拿到函数返回值(不是拿不到)==
std::async()创建异步任务。可能创建线程也可能不创建线程==,并且容易拿到线程入口函数的返回值;==

2、std::thread()是类模板,std::async()是函数模板,像

vector<thread>mythreads
vector<async>mythreads ×

3、由于系统资源限制:
①如果用std::thread创建的线程太多,则可能创建失败,系统报告异常,崩溃。

②如果用std::async,一般就不会报异常,因为如果系统资源紧张,无法创建新线程的时候,async不加额外参数的调用方式就不会创建新线程。而是在后续调用get()请求结果时执行在这个调用get()的线程上。

如果你强制async一定要创建新线程就要使用 std::launch::async 标记。承受的代价是,系统资源紧张时可能崩溃。

③根据经验,一个程序中线程数量 不宜超过100~200

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

不加额外参数的async调用时让系统自行决定,是否创建新线程。

std::future<int> result = std::async(mythread);
问题焦点在于这个写法,任务到底有没有被推迟执行。

通过wait_for()返回状态来判断:

简单示例:

std::future_status status = result.wait_for(std::chrono::seconds(6));
//std::future_status status = result.wait_for(6s);
	if (status == std::future_status::timeout) {
		//超时:表示线程还没有执行完
		cout << "超时了,线程还没有执行完" << endl;
	}
	else if (status == std::future_status::ready) {
		//表示线程成功放回
		cout << "线程执行成功,返回" << endl;
		cout << result.get() << endl;
	}
	else if (status == std::future_status::deferred) {
		cout << "线程延迟执行" << endl;
		cout << result.get() << endl;
	}

详细实例:

#include<iostream>
#include<thread>
#include<string>
#include<vector>
#include<list>
#include<mutex>
#include<future>
using namespace std;
 
int mythread() //线程入口函数
{
	cout << "mythread start" << "threadid= " << std::this_thread::get_id() << endl; //打印线程id
 
	std::chrono::milliseconds dura(5000); //定一个5秒的时间
	std::this_thread::sleep_for(dura);  //休息一定时常
 
	cout << "mythread end" << "threadid= " << std::this_thread::get_id() << endl; //打印线程id
 
	return 5;
}
int main()
{
	cout << "main" << "threadid= " << std::this_thread::get_id() << endl;
	std::future<int> result = std::async(mythread);//流程并不卡在这里
	cout << "continue....." << endl;
 
	//枚举类型
	std::future_status status = result.wait_for(std::chrono::seconds(0));//等待一秒
	
	if (status == std::future_status::deferred)
	{
		//线程被延迟执行了,系统资源紧张
		cout << result.get() << endl; //此时采取调用mythread(),get()等待线程结束并返回结果
	}
	else if (status == std::future_status::timeout)//
	{
		//超时:表示线程还没执行完;我想等待你1秒,希望你返回,你没有返回,那么 status = timeout
		//线程还没执行完
		cout << "超时:表示线程还没执行完!" << endl;
	}
	else if (status == std::future_status::ready)
	{
		//表示线程成功返回
		cout << "线程成功执行完毕,返回!" << endl;
		cout << result.get() << endl;
	}
 
	cout << "I love China!" << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值