async意味着异步执行代码,看如下示例:

#include <future>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
#include <exception> using namespace std; int do_something(char c)
{
std::default_random_engine dre(c);
std::uniform_int_distribution<int> id(10, 1000); for (int i = 0; i < 10; ++i)
{
this_thread::sleep_for(chrono::microseconds(id(dre)));
cout << c << ends;
} return c;
} int main() {
cout << "begin ..." << endl;
std::future<int> result = std::async(do_something, 'c');
cout << "waiting for result ..." << endl;
cout << "\n" << result.get() << endl;
cout << "finish!" << endl;
}

当对result调用get时,如果此时do_something函数还没有执行完毕,那么会导致main函数阻塞在这里,一直到该函数执行完毕。

 

async返回的是一个future对象,从字段意思上推测,结果是在未来的某一个时刻拿到。

async实际上是在背后偷偷的开启一个线程执行函数,但是上面的实例看不出来,于是我们写一个复杂一些的实例:

#include <future>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
#include <exception> using namespace std; int do_something(char c)
{
std::default_random_engine dre(c);
std::uniform_int_distribution<int> id(10, 1000); for (int i = 0; i < 10; ++i)
{
this_thread::sleep_for(chrono::microseconds(id(dre)));
cout << c << ends;
} return c;
} int func1()
{
return do_something('.');
} int func2()
{
return do_something('*');
} int main()
{
cout << "start ..." << endl;
// std::future<int> result1(std::async(std::launch::deferred, func1));
std::future<int> result1(std::async(func1)); int result2 = func2(); int result = result1.get() + result2; cout << "\nresult of func1() + func2(): " << result << endl;
}

这个程序的执行结果中 .和*乱序出现,说明func1和func2是乱序执行的,这是因为func1是在另一个线程中执行的。

C++11中async中future用法(一)的更多相关文章

  1. C#中async和await用法

    .net 4.5中新增了async和await这一对用于异步编程的关键字. async放在方法中存在await代码的方法中,await放在调用返回Task的方法前. class Class1 { pr ...

  2. (转)Spring中@Async用法总结

     原文:http://blog.csdn.net/blueheart20/article/details/44648667 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的: ...

  3. Spring中@Async用法总结

    引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3. ...

  4. Spring中@Async用法详解及简单实例

    Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...

  5. [翻译] Python 3.5中async/await的工作机制

    Python 3.5中async/await的工作机制 多处翻译出于自己理解,如有疑惑请参考原文 原文链接 身为Python核心开发组的成员,我对于这门语言的各种细节充满好奇.尽管我很清楚自己不可能对 ...

  6. SpringBoot中Async异步方法和定时任务介绍

    1.功能说明 Spring提供了Async注解来实现方法的异步调用. 即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行 ...

  7. 【转】Spring中@Async

    Spring中@Async 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实, ...

  8. C#中timer类的用法

    C#中timer类的用法 关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类 ...

  9. Spring中@Async注解实现“方法”的异步调用

    原文:http://www.cnblogs.com/zhengbin/p/6104502.html 简单介绍: Spring为任务调度与异步方法执行提供了注解支持.通过在方法上设置@Async注解,可 ...

随机推荐

  1. Selenium2+python自动化18-加载Firefox配置

    前言 有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile ...

  2. 技术英文单词贴--P

    P pagination 分页,页码 parse 解析,从语法上描述或分析 partial 局部的 pattern 模式 populate 填充,居住于,构成人口 precedence 优先 pref ...

  3. python profile

    一.profile,cProfile 1. python -m cProfile myprogram.py python -m profile myprog.py2. 使用import profile ...

  4. Oracle 的基本使用--基本命令<一>

    sql*plus 的常用命令 连接命令 1.conn[ect] 用法:conn 用户名/密码@网络服务名[as sysdba/sysoper]当用特权用户身份连接时,必须带上 as sysdba 或是 ...

  5. unreal3之FName及潜在bug

    FName是unreal3里对字符串高效处理的一种机制 基本原理就是把字符串hash存起来,然后每个字符串就只需要用一个数组索引值来表示了 FName的属性: NAME_INDEX Index; IN ...

  6. javascript学习第二课

    主要内容: 1.不可变的原始值和可变的对象引用 javascript中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着根本的区别.原始值是不可更改的;任何方法 ...

  7. IOS 代码块传值

    #import <UIKit/UIKit.h> typedef void (^MyBlock)(NSString*); @interface SecondViewController : ...

  8. u盘安装ubuntu

    安装ubuntu 准备工作:首先需要安装好UltraISO(版本要比较新,有些老版本有照做后不能安装的错误) 准备好一个U盘. 下载好ubuntu光盘.(我用的12.04版) 步骤: 1.制作安装U盘 ...

  9. treeMap and treeSet

    TreeSet:如果要对对象进行排序,对象类要实现Comparable接口! TreeMap:如果要对对象进行排序,对象类要实现Comparable接口! 下面是我自己写的小程序主要传输对象 publ ...

  10. Oracle DB 存储增强

    • 设置Automatic Storage Management (ASM)  快速镜像 再同步 • 使用ASM 首选镜像读取 • 了解可伸缩性和性能增强 • 设置ASM 磁盘组属性 • 使用SYSA ...