十一、atomic、async深入
一、原子操作
g++;
g+=1;
g = g+1;//结果不对
一般原子操作针对++,--,+=,&=,|=,^=是支持的,其他的可能不支持
二、std::async深入
用来创建异步任务。
#include <iostream>
#include <thread>
using namespace std; int mythread(){
cout<<"mythread is begining"<<endl;
return ;
} int main(){
std::future<int> result = std::async(mythread);//默认std::launch::async | std::launch::defered
cout<<result.get()<<endl;
}
1、async参数
延迟调用:std::launch::defered
#include <iostream>
#include <thread>
using namespace std; int mythread(){
cout<<"mythread is begining"<<endl;
return ;
} int main(){
std::future<int> result = std::async(std::launch::defered,mythread);//延迟调用,不创建新线程,只有调用了get/wait函数时,才会执行线程入口函数
cout<<result.get()<<endl;
}
强制创建一个线程 std::launch::async
#include <iostream>
#include <thread>
using namespace std; int mythread(){
cout<<"mythread is begining"<<endl;
return ;
} int main(){
std::future<int> result = std::async(std::launch::async,mythread);//强制异步任务在新线程上执行,意味着系统必须要创建出新线程来运行线程入口函数
cout<<result.get()<<endl;
}
std::launch::async | std::launch::defered
#include <iostream>
#include <thread>
using namespace std; int mythread(){
cout<<"mythread is begining"<<endl;
return ;
} int main(){
// |意味着,调用async的行为可能创建新线程,也有可能不创建,系统自行选择
std::future<int> result = std::async(std::launch::async | std::launch::defered,mythread);
cout<<result.get()<<endl;
}
不带参数同std::launch::async | std::launch::defered一样,系统会自行决定是异步(创建新线程)还是同步(不创建新线程)方式运行。
2、async和thread区别
thread是专门来创建线程的,如果系统资源紧张,thread创建线程可能会失败,执行therad时整个程序可能崩溃;而async不加额外参数的调用就不会创建新线程,而是后续谁调用了get函数来请求结果,这个异步任务mythread就运行在这条get语句所在的线程上(如get在main中,那么就相当于在主线程中调用mythread函数)
async一般不叫创建线程,叫创建一个异步任务。
最明显的不同:async有时候并不创建线程,例如上面的代码中,只有调用了get函数时,才会创建线程,否则就不会执行。
如果想要接thread返回的一个值,还必须设一个全局变量来赋值。async容易拿到线程函数的返回值
线程数量不能超过100~200,时间片的存在,
3、async不确定性
判断async有没有创建线程?
使用wait_for()
#include <iostream>
#include <thread>
using namespace std; int mythread(){
cout<<"mythread is begining"<<endl;
return ;
} int main(){
std::future<int> result = std::async(mythread);
//result.wait_for(10s),(10min)都可以
std::future_status status=result.wait_for(std::chrono::seconds());//0ms
if(status==std::future_status::deferred){
//没有立即创建线程,延迟执行了
cout<<result.get()<<endl;
}
else{
//线程没有延迟 //线程运行完了
if(status==std::future_status::ready){
//线程运行完了,成功返回
}
else if(status==std::future_status::timeout){
//超时,线程还没执行完,等线程执行完sleep }
} }
十一、atomic、async深入的更多相关文章
- ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
本篇博客我们来聊一下ReactiveSwift中的原子性操作,在此内容上我们简单的聊一下Posix互斥锁以及递归锁的概念以及使用场景.然后再聊一下Atomic的代码实现.Atomic主要负责多线程下的 ...
- spring boot 学习(十一)使用@Async实现异步调用
使用@Async实现异步调用 什么是”异步调用”与”同步调用” “同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行:”异步调用”则是只要上一行代码执行,无需等 ...
- qemu进程页表和EPT的同步问题
背景分析: 在之前分析EPT violation的时候,没有太注意qemu进程页表和EPT的关系,从虚拟机运行过程分析,虚拟机访存使用自身页表和EPT完成地址转换,没有用到qemu进程页表,所以也就想 ...
- asp.net core 系列之Dependency injection(依赖注入)
这篇文章主要讲解asp.net core 依赖注入的一些内容. ASP.NET Core支持依赖注入.这是一种在类和其依赖之间实现控制反转的一种技术(IOC). 一.依赖注入概述 1.原始的代码 依赖 ...
- 并发编程从零开始(十一)-Atomic类
并发编程从零开始(十一)-Atomic类 7 Atomic类 7.1 AtomicInteger和AtomicLong 如下面代码所示,对于一个整数的加减操作,要保证线程安全,需要加锁,也就是加syn ...
- ES6入门十一:Generator生成器、async+await、Promisify
生成器的基本使用 生成器 + Promise async+await Promise化之Promisify工具方法 一.生成器的基本使用 在介绍生成器的使用之前,可以简单理解生成器实质上生成的就是一个 ...
- linux基础-第十一单元 系统监控
第十一单元 系统监控 系统监视和进程控制工具-top和free top命令的功能 TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序 ...
- Gulp使用入门操作十一步压缩JS
前提需要安装nodejs 一. 全局安装Gulp npm install -g gulp 二.新建一个 gulpfile.js 文件 chapter2└── gulpfile.js 三.在 gulpf ...
- atomic, spinlock and mutex性能比较
我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...
随机推荐
- 四种方法给Vmware虚拟机清理瘦身
随着VMware虚拟机使用时间的增长,其所占用的空间也越来越大,本文来说说怎么给VMware虚拟机占用的空间进行瘦身. **方法一:VMware自带的清理磁盘 **这个方法是VMware自带,具有普适 ...
- laravel使用artisan报错SQLSTATE[42S02]: Base table or view not found: 1146
说明你在应用初始化阶段使用到了数据库层面的东西,然而当时数据库不存在这个表/字段,所以会报错 需要在初始化比如 config 目录配置中,使用了数据库,在使用前需要添加一层判断,如果不存在 你需要用到 ...
- ecshop后台增加模块菜单详细教程
我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...
- 【Linux】limits.conf 不重启就生效或者不生效的原因
前阵子,我要用到使LInux的文件打开数为65534个,而且需要永久生效,于是将配置写到了: vim /etc/security/limits.conf * soft nofile 65534* ha ...
- Noi2018 归途
zz:https://blog.csdn.net/dreaming__ldx/article/details/81106748 以海拔为第一关键字对边进行从大到小的排序,然后修建kruskal重构树, ...
- python基础-5 冒泡排序、递归
上节总结 一.上节内容补充回顾 1.lambda func = lambda x,y: 9+x 参数: x,y 函数体:9+x ==> return 9+x func: 函数名 def func ...
- python基础-7模块,第三方模块安装方法,使用方法。sys.path os sys time datetime hashlib pickle json requests xml
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- 各种sql驱动的相关配置
一.SqlServer数据库 1.sqlServer{2005,2008}软件 dataDriverName=com.microsoft.sqlserver.jdbc.SQLServerDriver ...
- hdu-2819.swap(二分匹配 + 矩阵的秩基本定理)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Spring Boot & ES 实战,值得参考!
作者:废物大师兄 cnblogs.com/cjsblog/p/9756978.html 1. 前言 1.1. 集成方式 Spring Boot中集成Elasticsearch有4种方式: REST C ...