#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. 设置zedgraph鼠标拖拽和局部放大属性(转帖)

    说一下几个属性的意义和具体应用: (1)鼠标拖拽显示区域 PanModifierKeys ->> Gets or sets a value that determines which mo ...

  2. LOJ 2542 「PKUWC2018」随机游走 ——树上高斯消元(期望DP)+最值反演+fmt

    题目:https://loj.ac/problem/2542 可以最值反演.注意 min 不是独立地算从根走到每个点的最小值,在点集里取 min ,而是整体来看,“从根开始走到点集中的任意一个点就停下 ...

  3. Log4j2的基本使用

    Log4j2是Log4j1.x的的升级版,其中也有很大的不同,最大的区别就是由以前的properties配置文件改为xml/json/yaml配置文件. 其中配置文件的位置官方说明如下: Log4j ...

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

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

  5. 【Spring学习笔记-5】Spring中的抽象bean以及bean继承

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

  6. 定义function的层级

    不知道标题拟的对不对,今天犯了个错误,图一是正确的写法. 图一 为了代码可以重复利用,我把其中两个方法独立出来,如图二. 图二 后来发现运行错误,说Gxrc未定义,百思不得其解,后来琢磨了好久,才发现 ...

  7. 为什么 JVM 不用 JIT 全程编译

    从知乎扣出来的内容 https://www.zhihu.com/question/37389356 作者:RednaxelaFX链接:https://www.zhihu.com/question/37 ...

  8. timus1745题解

    一.题目链接 http://acm.timus.ru/problem.aspx?space=1&num=1745 二.题意 给定$n$个由'('和')'组成的字符串,每个串最多只能使用$1$次 ...

  9. JS获取url传参

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  10. unity3d中物体的控制

    一.物体的循环移动和旋转 思路:通过对时间的计算,每隔一段时间让物体旋转,实现来回移动. float TranslateSpeed = 0.02f; float TranslateSpeedTime ...