一、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的更多相关文章

  1. 【转】C++ 11 并发指南一(C++ 11 多线程初探)

    引言 C++ 11自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些C++ 11的新特性,算是记录一下自己学到的东西吧,和大家共勉. 相信Linux程序员都用过Pthrea ...

  2. C++11 并发指南一(C++11 多线程初探)

    引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...

  3. c++11 多线程入门教程(一)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10945309.html 最近在找c++服务端开发的实习(大佬们有推荐吗QAQ..),恰好写了一 ...

  4. C++11 并发指南一(C++11 多线程初探)(转)

    引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...

  5. 【C/C++开发】C++11 并发指南一(C++11 多线程初探)

    引言 C++11 自2011年发布以来已经快两年了,之前一直没怎么关注,直到最近几个月才看了一些 C++11 的新特性,今后几篇博客我都会写一些关于 C++11 的特性,算是记录一下自己学到的东西吧, ...

  6. folly教程系列之:future/promise

         attension:本文严禁转载. 一.前言 promise/future是一个非常重要的异步编程模型,它可以让我们摆脱传统的回调陷阱,从而使用更加优雅.清晰的方式进行异步编程.c++11中 ...

  7. Future Promise 模式(netty源码9)

    netty源码死磕9  Future Promise 模式详解 1. Future/Promise 模式 1.1. ChannelFuture的由来 由于Netty中的Handler 处理都是异步IO ...

  8. C++11多线程教学(二)

    C++11多线程教学II 从我最近发布的C++11线程教学文章里,我们已经知道C++11线程写法与POSIX的pthreads写法相比,更为简洁.只需很少几个简单概念,我们就能搭建相当复杂的处理图片程 ...

  9. C++11多线程教学(一)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

随机推荐

  1. Hyperledger Fabric密码模块系列之BCCSP(三)

    fabric中通过工厂模式来生成bccsp实例,进而通过bccsp的接口来提供加密.解密.签名验证以及哈希等操作. fabric的factory工厂默认返回的bccsp实例是sw(也就是所有密码操作都 ...

  2. Vim 多行剪切、复制和删除

    剪切 快捷键方式: dd:剪切光标所处当前行 n + dd:剪切光标所在行及以下共 n 行 按 p 粘贴在光标所在行 命令行方式: 例如剪切1到10行,并粘贴在12行处: 1,10 m 12 复制 快 ...

  3. C# 快速释放内存的大数组

    本文告诉大家如何使用 Marshal 做出可以快速释放内存的大数组. 最近在做 3D ,需要不断申请一段大内存数组,然后就释放他,但是 C# 对于大内存不是立刻释放,所以就存在一定的性能问题. 在博客 ...

  4. mui 实用封装销毁页面

    /* * 描述:页面销毁封装 * 说明:针对订单特殊定位页面 * 返回首页,页面空白前进行销毁页面处理 * 使用:plusReady之后 */ (function(w) { var destructi ...

  5. 【Dubbo&&Zookeeper】3、Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    转自:http://blog.csdn.net/gaoshanliushui2009/article/details/50469595 我们公司使了阿里的dubbo,但是阿里的开源网站http://c ...

  6. Why does the C# compiler translate this != comparison as if it were a > comparison?

    Question: I have by pure chance discovered that the C# compiler turns this method: static bool IsNot ...

  7. MEF 插件式开发之 DotNetCore 初体验

    背景叙述 在传统的基于 .Net Framework 框架下进行的 MEF 开发,大多是使用 MEF 1,对应的命名空间是 System.ComponentModel.Composition.在 Do ...

  8. java.lang.IllegalArgumentException Expected MultipartHttpServletRequest

    解决方案:检查jar包,完毕无误后在Spring MVC的配置文件中加入: <!-- 需要文件上传功能时,启用以下配置 设置最大上传文件大小 10M=10*1024*1024(B)=104857 ...

  9. 深入研究HTML5实现图片压缩上传

    上篇文章中提到移动端上传图片,我们知道现在流量还是挺贵的,手机的像素是越来越高,拍个照动不动就是好几M,伤不起.虽然客户端可以轻轻松松实现图片压缩再上传,但是我们的应用还可能在浏览器里面打开,怎么办呢 ...

  10. 记一次wepy里面的渲染问题(this.$apply()的使用)

    今天在用wepy搞小程序的时候遇到了一个小坑,卡了我好一会,因为之前在做React,所以对wepy的了解不是特别深入,所以导致了这个问题的发生 先贴上来关键代码让大家看一看(备注之处是问题解决的方法) ...