//简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量
std::thread run([&](){
//执行一些耗时的操作
return 0;
});
run.detach();
    auto run=std::async([&](){
return this->执行一些耗时的操作成员函数();
});
run.get();
    auto run=std::async(std::launch::async,[&](){
return this->执行一些耗时的操作成员函数();
});
run.get();
auto future = std::async(std::launch::deferred, function, arg1, arg2);

// some time later, possibly in another thread:
future.get(); // calls the function with the arguments
// Console.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <thread> //线程库
#include <future>
#include <mutex>
#include<numeric> std::mutex g_display_mutex; void foo()
{
std::thread::id this_id = std::this_thread::get_id(); g_display_mutex.lock();
std::cout << "thread " << this_id << " sleeping...\n";
g_display_mutex.unlock(); std::this_thread::sleep_for(std::chrono::seconds(0));
} void threadTest()
{
std::thread t1(foo);
std::thread t2(foo);
t1.join();
t2.join();
} int sum(int &x, int &y)
{
std::cout << std::hex << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
return x + y;
} int sums(int x, int y,int z)
{
std::cout << std::hex << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
return x + y + z;
} int main()
{ int x = 3;
int y = 4;
std::future<int> fu = std::async(sums, 3, 4,5);
//std::future<int> fu = std::async(sum,std::ref(x),std::ref(y));
std::cout << fu.get() << std::endl; //获取当前计算机线程数量
std::cout << std::thread::hardware_concurrency() << std::endl;
//获取当前线程ID
std::cout << std::hex <<std::this_thread::get_id() << std::endl;
system("pause");
return 0;
}

c++ 如何获取多线程的返回值?(std::thread ,std::async)的更多相关文章

  1. c++ 如何获取多线程的返回值?

    // Console.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include ...

  2. python获取多线程的返回值

    import threading class MyThread(threading.Thread): def __init__(self,func,args=()): super(MyThread,s ...

  3. python使用threading获取线程函数返回值的实现方法

    python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...

  4. Java多线程带返回值的Callable接口

    Java多线程带返回值的Callable接口 在面试的时候,有时候是不是会遇到面试会问你,Java中实现多线程的方式有几种?你知道吗?你知道Java中有可以返回值的线程吗?在具体的用法你知道吗?如果两 ...

  5. 统计文件种类数+获取子shell返回值的其它方法

    前言 只是作为一个shell的小小练习和日常统计用,瞎折腾的过程中也是摸到了获取子shell返回值的几种方法: 肯定还有别的方法,跟进程间的通信相关,希望你能提出建议和补充,谢谢~ 完整程序: #! ...

  6. 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)

    1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...

  7. web3调用call()方法获取不到返回值

    一.web3的call()获取不到返回值问题和解决方法 在彩票小合约中,遇到一个问题:合约中 有两个方法 第一个返回一个账户地址,没有使用到当前方法调用者信息: 第二个使用到了当前方法调用者信息 在w ...

  8. 获取Java线程返回值的几种方式

    在实际开发过程中,我们有时候会遇到主线程调用子线程,要等待子线程返回的结果来进行下一步动作的业务. 那么怎么获取子线程返回的值呢,我这里总结了三种方式: 主线程等待. Join方法等待. 实现Call ...

  9. 利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程

    1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器 ...

随机推荐

  1. npm错误:Error: listen EADDRNOTAVAIL

    错误 Error: listen EADDRNOTAVAIL 127.0.0.1:8080 有两种情况 8080端口被绑定了 地址错误 Error: getaddrinfo ENOTFOUND 域名错 ...

  2. 根据IP获取国家

    国外的还算比较权威的IP地址库,而且免费,每天调用次数不超过1000免费.超过另收费. public string Ip2Country(string ip) { try { string url = ...

  3. ORACLE 11.2.0.4 OCR VOTING DISK 模拟恢复场景

    ① 备份   ocrconfig -export 文件名   或者   ocrconfig -manualbackup   或者   找到备份      ocrconfig -local -showb ...

  4. 面包屑 CSS

    首先祝大家,在新年里,好的.善良的都都马上有. 关于css实现面包屑已经是一个典型例子了.不过今天还是与大家分享一下实现的原理.原理:每一段元素固定宽度并向左浮动,头尾突出部分相对父元素用相对定位固定 ...

  5. [Full-stack] 快速上手开发 - React

    故事背景 [1] 博客笔记结合<React快速上手开发>再次系统地.全面地走一遍. [2] React JS Tutorials:包含了JS --> React --> Red ...

  6. 二叉树的遍历--C#程序举例二叉树的遍历

    二叉树的遍历--C#程序举例二叉树的遍历 关于二叉树的介绍笨男孩前面写过一篇博客 二叉树的简单介绍以及二叉树的存储结构 遍历方案 二叉树的遍历分为以下三种: 先序遍历:遍历顺序规则为[根左右] 中序遍 ...

  7. Error running app: Default Activity Not Found

    最近在调试安装Android Widget程序时,碰到Error running app: Default ActivityNot Found. 因为简单的Widget程序,如果不和应用程序关联,就不 ...

  8. ftp服务器Serv-U 设置允许自动创建不存在的目录

    一.由来 最近改写了项目中ftp上传部分的代码. 用到的组件为: <dependency> <groupId>commons-net</groupId> <a ...

  9. 微信小游戏 RES版本控制+缓存策略 (resplugin和ResSplitPlugin插件使用)

    参考: RES版本控制 使用 AssetsManager 灵活定制微信小游戏的缓存策略 一.我们的目标 目标就是让玩家快速进入游戏,然后根据游戏的进度加载相应的资源,并可对资源进行版本控制.本地缓存. ...

  10. 使用sts(SpringToolSuite4)无法将项目部署到tomcat容器

    一般情况下maven项目不能添加到tomcat容器中 ,需要在项目上进行设置 但是sts没有安装此插件,可以改用eclipse进行开发.