C++11并发与多线程笔记(11)atomic续谈、async深入谈

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

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

  • 示例代码:

    #include <iostream>
    #include <thread>
    #include <future>
    using namespace std;
    
    //我们封装了一个类型为int的对象(值),具有原子操作性质,我们可以像操作一个int类型变量一样来操作这个g_mycount
    std::atomic<int> g_mycount = 0;
    
    void mythread()			//线程入口函数
    {
    	for (int i = 0; i < 1000000; i++)
    	{
    		//g_mycount++;		//对应的操作室原子操作,不会被打断
    		//g_mycount += 1;		//结果和g_mycount++相同
    		g_mycount = g_mycount + 1;		//结果不对
    	}
    	return;
    }
    
    int main()
    {
    
    	thread mytobj1(mythread);
    	thread mytobj2(mythread);
    	mytobj1.join();
    	mytobj2.join();
    
    	cout << "两个线程执行完毕,最终g_mycount的结果是:" << g_mycount << endl;
    
    	cout << "I love China!" << endl;
    
    	return 0;
    }
    

    结果如下:

    image-20211105183731340

    结果为错误的,说明sdt::atomic原子操作不支持g_mycount = g_mycount + 1这种形式。

2、std::async深入谈

2.1 std::async用来创建一个异步任务

std::future<int> result = std::async(mythread);

std::lanuch::deferred

std::future<int> result = std::async(std::launch::deferred, mythread);
  • std::async()的第一个参数位置,作用:延迟调用,并且不创建新线程,延迟到future对象调用get()或者wait()的时候才执行mythread(),如果没有调用get()或者wait()mythread()不会执行。

std::lanuch::async

std::future<int> result = std::async(std::launch::async, mythread);
  • std::async()的第一个参数位置,作用:强制这个异步任务在新线程上执行,这意味着系统必须要给我创建出新线程来执行mythread()

launch::async | launch::deferred

std::future<int> result = std::async(std::launch::deferred | std::launch::async, mythread);
  • Automatic: The function chooses the policy automatically (at some point). This depends on the system and library implementation, which generally optimizes for the current availability of concurrency in the system.

  • 自动:该函数自动(在某个点)选择策略。这取决于系统和库实现,通常会针对系统中并发的当前可用性进行优化。

  • 意味着调用async的行为可能是“创建新线程并立即执行”或者“没有创建新线程并且延迟到调用get()才开始执行任务入口函数,两者局其一。

不带额外参数,只给async函数一个入口名函数

std::future<int> result = std::async(std::launch::deferred | std::launch::async, mythread);
  • 会使用默认值,默认值为launch::async | launch::deferred

  • 系统会自行决定是异步(创建新线程)还是同步(不创建新线程)方式运行。

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

  • std::thread():如果系统资源紧张,那么可能std::thread()创建线程就会失败,那么执行std::thread()时整个程序可能崩溃。
  • std::async()我们一般不叫创建线程(即使async能够创建线程),我们一般叫它创建一个异步任务。
  • std::async()std::thread()最明显的不同就是,std::async()有时候并不创建新线程。
  • std::thread()创建线程的方式,如果有线程返回值,想拿到这个值也不容易。
  • std::async()创建异步任务,可能创建线程也可能不创建线程,但std::async()调用方法很容易拿到线程入口函数的返回值。
  • 由于系统资源限制:
    • 如果用std::thread创建的线程太多,则可能创建失败,系统报告异常,崩溃。
    • 如果用std::async,一般就不会报异常或者崩溃,因为,当系统资源紧张导致无法创建新线程的时候,std::async这种不加额外参数的调用就不会创建新线程,而是后续谁调用了get()来请求结果,那么这个异步任务就运行在执行这条get()语句所在的线程上。
    • 如果强制使用std::async一定要创建新线程时,那么就必须使用std::lanuch::async。承受的代价就是系统资源紧张时,程序崩溃。
  • 经验:一个程序里,线程数量不宜超过100-200。

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

  • 使用std::future::wait_for()函数进行状态判断

示例代码:

#include <iostream>
#include <thread>
#include <future>
using namespace std;

int mythread()
{
	cout << "mythread() start " << " threadid = " << std::this_thread::get_id() << endl;
	
	std::chrono::milliseconds dura(5000);	//5s
	std::this_thread::sleep_for(dura);
	
	return 1;
}

int main()
{
	cout << "main start " << " threadid = " << std::this_thread::get_id() << endl;
	std::future<int> result = std::async(mythread);

	std::future_status status = result.wait_for(0s);	//0s:零秒相当于std::chrono::microseconds(0)

	if (status == std::future_status::deferred)
	{
		//任务被延迟了(系统资源紧张,采用了std::lanuch::deferred策略了)
		cout << result.get() << endl;
	}
	else
	{
		//任务没有被推迟,已经开始运行了,线程被创建了
		if (status == std::future_status::ready)
		{
			cout << "线程成功执行完毕并返回!" << endl;
			cout << result.get() << endl;
		}
		else if (status == std::future_status::timeout)
		{
			cout << "超时,线程还没执行完,稍等" << endl;
			cout << result.get() << endl;
		}
	}

	return 0;
}

注:本人学习c++多线程视频地址:C++多线程学习地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值