std::async和std::future

std::async创建一个后台线程执行传递的任务,这个任务只要是callable object均可,然后返回一个std::future。future储存一个多线程共享的状态,当调用future.get时会阻塞直到绑定的task执行完毕:

#include <iostream>
#include <future> void task() {
for (int i = 0; i < 10; i++) {
std::cout << "A";
}
} int main() {
std::future<void> result{ std::async(task) };
for (int i = 0; i < 10; i++) {
std::cout << "B";
}
result.get(); //强制阻塞main线程,直到task线程执行完毕
system("pause");
return 0;
}

std::launch::async

上面task返回void,这个结果没用,我们只是单纯的想等待任务线程结束。

对这种需求还可以用更简单的方法:指定一个launch policy

#include <iostream>
#include <future> void task() {
for (int i = 0; i < 10; i++) {
std::cout << "A";
}
} int main() {
std::future<void> result{ std::async(std::launch::async,task) };
for (int i = 0; i < 10; i++) {
std::cout << "B";
}
system("pause");
return 0;
}

在创建async的时候指定一个launch policy,连result.get都可以不用了,不过还是需要把async的返回值赋给result。如果不赋值async会和同步调用一样在这里阻塞直到调用完毕,相当于没用async。

总共有两种launch policy:

  • std::launch::async 当返回的future失效前会强制执行task,即不调用future.get也会保证task的执行
  • std::launch::deferred 仅当调用future.get时才会执行task

    如果创建async时不指定launch policy,他会默认std::launch::async|std::launch::deferred,根据情况选一种执行

std::launch::deferred

再来试试std::launch::deferred策略。

#include <iostream>
#include <future> void task() {
for (int i = 0; i < 10; i++) {
std::cout << "A";
}
} int main() {
std::future<void> result{ std::async(std::launch::deferred,task) };
for (int i = 0; i < 10; i++) {
std::cout << "B";
}
result.get();
system("pause");
return 0;
}

程序输出BBBBBBBBBBAAAAAAAAAA,和我们说的一样,创建async的时候它并没有开启新线程执行任务,而是等到result.get的时候才执行

C++并发高级接口:std::async和std::future的更多相关文章

  1. C++11 使用异步编程std::async和std::future

    先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...

  2. C++并发低级接口:std::thread和std::promise

    std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...

  3. std::async的使用总结

    C++98标准中并没有线程库的存在,直到C++11中才终于提供了多线程的标准库,提供了管理线程.保护共享数据.线程间同步操作.原子操作等类.多线程库对应的头文件是#include <thread ...

  4. 第26课 std::async异步任务

    一. std::async函数模板 (一)std::async和std::thread的区别 1. 两者最明显的区别在于async采用默认启动策略时并不一定创建新的线程.如果系统资源紧张,那么std: ...

  5. c++ 如何获取多线程的返回值?(std::thread ,std::async)

    //简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...

  6. 【C++并发实战】(三) std::future和std::promise

    std::future和std::promise std::future std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::f ...

  7. The promises and challenges of std::async task-based parallelism in C++11 C++11 std::async/future/promise

    转载 http://eli.thegreenplace.net/2016/the-promises-and-challenges-of-stdasync-task-based-parallelism- ...

  8. C++11之std::future和std::promise和std::std::packaged_task

    为什么C++11引入std::future和std::promise?C++11创建了线程以后,我们不能直接从thread.join()得到结果,必须定义一个变量,在线程执行时,对这个变量赋值,然后执 ...

  9. C++并发编程之std::async(), std::future, std::promise, std::packaged_task

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

随机推荐

  1. [转]安卓新一代多渠道打包工具Walle 解决渠道包V2签名问题

    转自https://www.jianshu.com/p/572b59829a08 为什么要打多个渠道的包? 大家都知道,android应用商店大大小小有几百个,作为一个有志向的app,就需要做到统计各 ...

  2. idea的spring boot项目,运行时不要显示在dashboard中

    将对应项目的上图配置,取消勾选即可.

  3. List里边存放Object对象获取方式

    if (tableListt != null && tableListt.size() > 0) { for (int i = 0; i < tableListt.size ...

  4. 原生js中实现全选和反选功能

    <!DOCTYPE html>      <html>      <head lang="en">          <meta char ...

  5. HTML笔记04---计时事件

    JavaScript运动01 计时事件 1.语法:var t=setTimeout("javascript语句",毫秒); setTimeout() 方法会返回某个值.在上面的语句 ...

  6. [LeetCode] Construct Binary Tree from String 从字符串创建二叉树

    You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...

  7. [SDOI2014]重建

    题目描述 T国有N个城市,用若干双向道路连接.一对城市之间至多存在一条道路. 在一次洪水之后,一些道路受损无法通行.虽然已经有人开始调查道路的损毁情况,但直到现在几乎没有消息传回. 辛运的是,此前T国 ...

  8. hdu 5392

    Sample Input 2 3 1 3 2 6 2 3 4 5 6 1   Sample Output 2 6 题意:给一个转置求它的循环长度 题解:分解成循环求最小公倍数 #include< ...

  9. RabbitMQ-Spring AMQP

    上篇文章RabbitMQ基础入门学习了rabbitMQ一些基础的api,当然spring也在原生代码的基础上做了更多的封装,这篇文章就基于spring-rabbit,学习一下spring的实现. p. ...

  10. gcc编译器的工作流程

    参考资料:http://www.cnblogs.com/dfcao/p/csapp_intr1_1-2.html 在linux系统上,从源文件到目标文件的转化是由编译器完成的.以hello.c程序的编 ...