C++ 0x std::async 的应用
#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 的应用的更多相关文章
- 用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- C++并发高级接口:std::async和std::future
std::async和std::future std::async创建一个后台线程执行传递的任务,这个任务只要是callable object均可,然后返回一个std::future.future储存 ...
- C++11 使用异步编程std::async和std::future
先说明一点:std::asyanc是std::future的高级封装, 一般我们不会直接使用std::futrue,而是使用对std::future的高级封装std::async. 下面分别说一下. ...
- 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相当 ...
- c++ 如何获取多线程的返回值?(std::thread ,std::async)
//简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...
- 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列
C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...
- C++11 使用 std::async创建异步程序
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- C++并发编程之std::async(), std::future, std::promise, std::packaged_task
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- (原创)用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + ); t.join(); 但是线程毕竟是属于比较 ...
随机推荐
- FineUI与百度地图简单示例 (转帖)
http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=4191&extra=page%3D1 前台代码 <%@ Page ...
- Mybatis连接Oracle实现增删改查实践
1. 首先要在项目中增加Mybatis和Oracle的Jar文件 这里我使用的版本为ojdbc7 Mybatis版本为:3.2.4 2. 在Oracle中创建User表 create table T_ ...
- sublime快捷键功能记录
shift+tab 向前缩进 ctrl+shift+k 删除当前行 菜单栏 view--side bar 选项 控制左侧文件展示视图 菜单栏“view”——“hideMiniMap” or &qu ...
- CefSharp v62修改,支持.net4.0
吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎 ...
- 如何在CentOS中添加Swap
1.检查 Swap 空间 在设置 Swap 文件之前,有必要先检查一下系统里有没有既存的 Swap 文件.运行以下命令: 1 swapon -s 如果返回的信息概要是空的,则表示 Swap 文件不存在 ...
- Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(五)技能属性集(AttributeSet)
如果没有完成前面的教程,请前往学习.先上一段理论介绍(源于https://wiki.unrealengine.com/GameplayAbilities_and_You#GameplayTasks): ...
- JQUERY dialog的用法详细解析
本篇文章主要是对JQUERY中dialog的用法进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 今天用到了客户端的对话框,把 jQuery UI 中的对话框学习了一下. 准备 jQ ...
- Android RIL Architecture
Android RIL Architecture by Gomathi Sankar Introduction The Article explains about the buildin ...
- html5 如何实现客户端验证上传文件的大小
在HTML 5中,现在可以在客户端进行文件上传时的校验了,比如用户选择文件后,可以 马上校验文件的大小和属性等.本文章向码农介绍html5 如何实现客户端验证上传文件的大小,感兴趣的码农可以参考一下. ...
- shell语法(二)
Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...