C++11多线程のfuture,promise,package_task
一、c++11中可以在调用进程中获取被调进程中的结果,具体用法如下
// threadTest.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
#include <deque>
#include <condition_variable>
#include <future> using namespace std; int factorial(int n)
{
int res = ;
for (int i = n; i > ; i--)
res *= i;
return res;
} int main()
{
int x; std::future<int> fu = std::async(factorial,);
x = fu.get(); cout << "result is " << x << endl;
std::getchar();
return ;
}
二、promise将值传递给子线程
// threadTest.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
#include <deque>
#include <condition_variable>
#include <future> using namespace std; int factorial(future<int> &f)
{
int n = f.get();
int res = ;
for (int i = n; i > ; i--)
res *= i;
return res;
} int main()
{
int x;
std::promise<int> p;
std::future<int> f = p.get_future(); std::future<int> fu = std::async(std::launch::async ,factorial, std::ref(f));
p.set_value();
x = fu.get(); cout << "result is " << x << endl;
std::getchar();
return ;
}
有promise的情况下必须有setvalue;promise和future均不能被复制,只能被移动。
如果我们需要多个子线程执行一段代码,那么可以使用std::shared_future()来创建sf,来获取一个变量。
三、可调用对象
// threadTest.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
#include <deque>
#include <condition_variable>
#include <future> using namespace std; class A
{
public:
void f( int a,char c) {}
int operator()(int n) { return ; } private: }; void foo(int x) {} int main()
{
A a; std::thread t1(a, ); //传递a的copy给子线程
std::thread t2(std::ref(a),);//传递a的引用给子线程
std::thread t3(std::move(a),);//a在主线程已经失效
std::thread t4(A(), ); //传递临时创建的对象给子线程 std::thread t5(foo,); //传递一个函数给
std::thread t6([](int x) {}, ); //传递一个lambd给子线程
std::thread t7(&A::f,a,,"w"); //传递a的copy的成员函数给子线程
std::thread t7(&A::f, &a, , "w"); std::getchar();
return ;
}
四、package_task异步访问可调用对象的结果
C++11多线程のfuture,promise,package_task的更多相关文章
- 【转】C++ 11 并发指南一(C++ 11 多线程初探)
引言 C++ 11自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些C++ 11的新特性,算是记录一下自己学到的东西吧,和大家共勉. 相信Linux程序员都用过Pthrea ...
- C++11 并发指南一(C++11 多线程初探)
引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...
- c++11 多线程入门教程(一)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10945309.html 最近在找c++服务端开发的实习(大佬们有推荐吗QAQ..),恰好写了一 ...
- C++11 并发指南一(C++11 多线程初探)(转)
引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...
- 【C/C++开发】C++11 并发指南一(C++11 多线程初探)
引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...
- folly教程系列之:future/promise
attension:本文严禁转载. 一.前言 promise/future是一个非常重要的异步编程模型,它可以让我们摆脱传统的回调陷阱,从而使用更加优雅.清晰的方式进行异步编程.c++11中 ...
- Future Promise 模式(netty源码9)
netty源码死磕9 Future Promise 模式详解 1. Future/Promise 模式 1.1. ChannelFuture的由来 由于Netty中的Handler 处理都是异步IO ...
- C++11多线程教学(二)
C++11多线程教学II 从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程 ...
- C++11多线程教学(一)
本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...
随机推荐
- Java Generator
以前我以为只有Python才会有generator,看来当时的我才年轻,后来认真研读<Thinking in Java>之后大有感悟,原来Java亦有generator,故做一次记录分享. ...
- .net实现支付宝在线支付
流程参考<实物商品交易服务集成技术文档2.0.pdf>网关地址http://paytest.rupeng.cn/AliPay/PayGate.ashx 网关参数说明:partner:商户编 ...
- 【ichart】简单的统计图表ichart.js的使用
1.首先下载,点击下载 2.只需要这一个js,粘贴赋值到自己项目中即可. 3.引入js <script type="text/javascript" src=" ...
- mysql游标中使用临时表
有时候需我们要组合几张表的数据,在存储过程中,经过比较复杂的运算获取结果直接输出给调用方,比如符合条件的几张表的某些字段的组合计算,mysql临时表可以解决这个问题. 所谓临时表:只有在当前连接情况下 ...
- How do I close a single buffer (out of many) in Vim?
I open several files in Vim by, for example, running vim a/*.php which opens 23 files. I then make m ...
- blfs(systemd版本)学习笔记-配置远程连接显示中文
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 远程连接的lfs系统需要具备以下环境便可在xshell或其他远程终端上面显示中文: 1.lfs主机设置中文编码(需要配置) 2. ...
- js之模态对话框
目标效果:点击页面按钮,显示模态对话框,在模态对话框里点击取消关闭模式对话框. 效果如下 实现代码如下: <!DOCTYPE html> <html lang="en&qu ...
- 【代码笔记】Web-ionic-列表
一,效果图. 二,index.html代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- python之管道, 事件, 信号量, 进程池
管道:双向通信 2个进程之间相互通信 from multiprocessing import Process, Pipe def f1(conn): from_zjc_msg = conn.recv( ...
- IDEA项目搭建八——使用MybatisPlus简化数据库交互
一.MybatisPlus简化数据库交互 我们使用Mybatis发现需要在mapper.xml中写很多重复的简单CRUD(增删改查),使用MybatisPlus可以大大简化这部分代码,官方文档http ...