#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <algorithm>
#include <future>
using namespace std; /*
知识点:
this_thread : 当前线程
chrono::system_clock::now() : 系统的当前时间点
future<void> async(std::launch::async,[](){...}) : 启动一个任务,返回的是一个 future 但一般直接 auto
this_thread::sleep_for(chrono::seconds(1)) : 将当前线程休眠1秒
chrono::duration_cast<chrono::milliseconds>(end - begin).count() : 计算两个时间点之间的差值
*/ // 打印出当前线程的 ID
void PrintThreadID(const char* name) {
cout << name << " Thread id is " << this_thread::get_id() << endl;
} int main() {
// 得到开始时的系统时间
auto begin = chrono::system_clock::now(); /*
可以通过stl::async函数的第一个参数控制任务的并行方式,它有如下三个取值:
launch::async : 异步方式。这个方式下,所有任务都会新启动一个线程执行
launch::deferred : 同步方式。 这个方式下,任务不会新启动线程,串行在创建任务的线程中执行。
launch::any : 综合方式。 这个方式下,会复用创建任务的线程。 (默认值)
*/ // 启动第一个任务
auto task1 = async([]{
PrintThreadID("Task1");
this_thread::sleep_for(chrono::seconds());
return ;
}); // 启动第二个任务
auto task2 = async([]{
PrintThreadID("Task2");
this_thread::sleep_for(chrono::seconds());
return ;
}); // get() 方法,本身就是
cout << task1.get() + task2.get() << endl; // 得到结束时的系统时间
auto end = chrono::system_clock::now(); // 这里是计算开始与结束之间的时间差
cout << "Spend Time : " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << endl; return ;
}

C++ 0x std::async 的应用的更多相关文章

  1. 用C++11的std::async代替线程的创建

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

  2. C++并发高级接口:std::async和std::future

    std::async和std::future std::async创建一个后台线程执行传递的任务,这个任务只要是callable object均可,然后返回一个std::future.future储存 ...

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

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

  4. C++ std::async vs async/await in C# - Stack Overflow

    C++ std::async vs async/await in C# - Stack Overflow 我想知道新的c ++功能std::async是否与两个C#关键字async / await相当 ...

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

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

  6. 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列

    C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...

  7. C++11 使用 std::async创建异步程序

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

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

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

  9. (原创)用C++11的std::async代替线程的创建

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

随机推荐

  1. XFire构建web service客户端的五种方式

    这里并未涉及到JSR 181 Annotations 的相关应用,具体的三种方式如下 ① 通过WSDL地址来创建动态客户端 ② 通过服务端提供的接口来创建客户端 ③ 使用Ant通过WSDL文件来生成客 ...

  2. win10激活命令

    以管理员方式打开命令提示符输入以下3条命令slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX 按回车slmgr /skms 54.223.212.31 按回车slmgr ...

  3. c# 模拟POST上传文件到服务器

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  4. ASP.NET ASHX中获得Session

    有时候需要在ASHX中获取Session,可是一般是获取不到的,如何解决? 1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和 ...

  5. VS2005的depends工具 (分析EXE)

    忙乎了近两个月,程序开始打包供外部调用了,连同其所需的dll文件,这就需要使用VC自带的Depends软件,在VS2005中其路径为:D:\Program Files\Microsoft Visual ...

  6. vim应用:终极解决windows系统gvim/vim的各种乱码(文件,菜单,提示信息)!

    这个方法解决了我的windows下 gvim的中文乱码问题(跟大家分享一下). 此方法引用   http://www.douban.com/note/145491549/ 查看文件的编码::echo ...

  7. java newInstance() 的参数版本与无参数版本详解

    newInstance() 的参数版本与无参数版本详解 博客分类: Core Java   通过反射创建新的类示例,有两种方式: Class.newInstance() Constructor.new ...

  8. BASIC-8_蓝桥杯_回文数

    示例代码: #include <stdio.h> int main(void){ int i = 0 ; int a = 0 , b = 0 , c = 0 , d = 0 ; for ( ...

  9. 全局 SqlConnection

    class SqlHelper { public static SqlConnection conn; public static SqlConnection Open(string connStr) ...

  10. tomcat 加载顺序 web.xml文件详解

    一. 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Se ...