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(); 但是线程毕竟是属于比较 ...
随机推荐
- Elasticsearch 基础入门
原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...
- svn权限设置
原文:http://swjr.blog.com.cn/archives/2006/TheRoadToSubversion1authz.shtml http://www.dayuer.com/freeb ...
- [OI向?] ubuntu下一些常用的技巧
想起来什么就写什么吧. Ubuntu下的对拍程序 python是最为简便的. from os import system while True: system("./make > in ...
- bzoj3326: [Scoi2013]数数
Description Fish 是一条生活在海里的鱼,有一天他很无聊,就开始数数玩. 他数数玩的具体规则是: 1. 确定数数的进制B 2. 确定一个数数的区间[L, R] 3. 对于[L, R] 间 ...
- 显式等待大结局___封装成API方便控制层调用
控制层 测试用例层: 控制层示例代码: #coding=utf-8from selenium.webdriver.common.by import Byfrom selenium.webdriver. ...
- CA单向认证和双向认证的区别?
1:单向认证,内容会被串改吗?
- ORA-03113:通信通道的文件结尾
问题: 用命令startup启动实例时,报错“ORA-03113:通信通道的文件结尾”. 解决: SQL> startup mount ORACLE 例程已经启动. Total System G ...
- 【POJ】2480 Longge's problem(欧拉函数)
题目 传送门:QWQ 分析 题意就是求∑gcd(i, N) 1<=i <=N.. 显然$ gcd(i,n) = x $时,必然$x|n$. 所以我们枚举一下n的约数,对于每个约数x,显然$ ...
- 文件读操作(IO编程)
将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流——InputStream或Reader.而由于读取的是特定的数据源——文件,则可以使用输入对应的子类FileInputStream ...
- 文件系统性能测试--iozone
iozone 一个文件系统性能评测工具,可以测试Read, write, re-read,re-write, read backwards, read strided, fread, fwrite, ...