一、原子操作

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深入的更多相关文章

  1. ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁

    本篇博客我们来聊一下ReactiveSwift中的原子性操作,在此内容上我们简单的聊一下Posix互斥锁以及递归锁的概念以及使用场景.然后再聊一下Atomic的代码实现.Atomic主要负责多线程下的 ...

  2. spring boot 学习(十一)使用@Async实现异步调用

    使用@Async实现异步调用 什么是”异步调用”与”同步调用” “同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行:”异步调用”则是只要上一行代码执行,无需等 ...

  3. qemu进程页表和EPT的同步问题

    背景分析: 在之前分析EPT violation的时候,没有太注意qemu进程页表和EPT的关系,从虚拟机运行过程分析,虚拟机访存使用自身页表和EPT完成地址转换,没有用到qemu进程页表,所以也就想 ...

  4. asp.net core 系列之Dependency injection(依赖注入)

    这篇文章主要讲解asp.net core 依赖注入的一些内容. ASP.NET Core支持依赖注入.这是一种在类和其依赖之间实现控制反转的一种技术(IOC). 一.依赖注入概述 1.原始的代码 依赖 ...

  5. 并发编程从零开始(十一)-Atomic类

    并发编程从零开始(十一)-Atomic类 7 Atomic类 7.1 AtomicInteger和AtomicLong 如下面代码所示,对于一个整数的加减操作,要保证线程安全,需要加锁,也就是加syn ...

  6. ES6入门十一:Generator生成器、async+await、Promisify

    生成器的基本使用 生成器 + Promise async+await Promise化之Promisify工具方法 一.生成器的基本使用 在介绍生成器的使用之前,可以简单理解生成器实质上生成的就是一个 ...

  7. linux基础-第十一单元 系统监控

    第十一单元 系统监控 系统监视和进程控制工具-top和free top命令的功能 TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序 ...

  8. Gulp使用入门操作十一步压缩JS

    前提需要安装nodejs 一. 全局安装Gulp npm install -g gulp 二.新建一个 gulpfile.js 文件 chapter2└── gulpfile.js 三.在 gulpf ...

  9. atomic, spinlock and mutex性能比较

    我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...

随机推荐

  1. 类ThreadGroup

    Java中使用ThreadGroup来表示线程组,它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. 默认的情况下,所有的线程都属于主线程组. public final Thread ...

  2. VS2012下自定义打开文件对话框

    VS2012下自定义打开文件对话框,MFC的CFileDialog封装了太多,太复杂,绕得头晕,自己封装一个得了 #pragma once #include <objbase.h> #in ...

  3. 40 insert语句的锁

    40 insert语句的锁 上一篇文章中对mysql自增主键锁做了优化,尽量在申请到自增id后,就释放自增锁. 因此,insert语句是一个很轻量的操作,不过,这个结论对于”普通的insert”才生效 ...

  4. Java ——Scanner

    本节重点思维导图 import java.util.Scanner;//导包 public class Demo { public static void main(String[] args) { ...

  5. Python笔记(二十二)_魔法方法_基本魔法方法

    __init__(self[,...]) __init__和__new__组成python的构造器,但__init__更多的是负责初始化操作,相当于一个项目中的配置文件,__new__才是真正的构造函 ...

  6. 查询IP地址

    在黑窗口里面输入:ipconfig

  7. NoSQL基础学习

    NoSQL基础学习 最近学习的第一个Nosql就是Mongodb,为了了解Nosql的基本知识,特地总结,主要是学习Nosql的理论 一.Introduction(介绍) 它是“ Not Only S ...

  8. “希希敬敬对”队软件工程第九次作业-beta冲刺第二次随笔

    队名:  “希希敬敬对” 龙江腾(队长) 201810775001 杨希                   201810812008 何敬上 201810812004 今日讨论会议照片一张: 每个人 ...

  9. Mybatis-学习笔记(2)Mybatis配置文件

      3>typeAliases:类型别名.2种指定方式. 1>给某个类起个别名 <typeAliases> <typeAlias type="com.lfy.b ...

  10. POJ-3468 A Simple Problem with Integers (区间求和,成段加减)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...