//简单的 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;
}
#include <thread>
#include <memory>
#include <string>
#include <future>
#include <mutex>
#include<numeric>
#include <cstdlib>
#include <iostream> int main()
{
std::cout << "start 当前 main 线程ID:" << std::this_thread::get_id() << std::endl; // 定义函数原型
auto fn = [](const char *info)->int {
int sum = 0;
for (int i = 0; i < 1000000000; ++i) {
++sum;
}
std::cout << std::string(info) << " 线程ID:" << std::this_thread::get_id() << " sum:" << sum << std::endl;
return sum;
}; // 异步求值
std::future<int> calc_async = std::async(std::launch::async,fn,"异步求值"); // 惰性求值
std::future<int> calc_deferred = std::async(std::launch::deferred,fn,"惰性求值"); std::cout << "end 当前 main 线程ID:" << std::this_thread::get_id() << std::endl; // 启动线程
std::cout << "惰性求值结果:" << calc_deferred.get() << std::endl; /*!
运行以上代码可以看出,惰性求值 方式需要调用 calc_deferred.get() 才会执行该函数,并且线程id和主线程是一致的.
而异步求值会自动运行,当然你想获取结果也可以使用 calc_async.get();
*/ getchar();
return 0;
}
int main()
{
std::cout << "当前线程ID:" << std::this_thread::get_id() << std::endl; std::mutex mutex;//互斥锁
std::condition_variable cv;//信号量 auto fn_worker_thread = [&](int id) {
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock); std::cout << "线程ID:" << std::this_thread::get_id() << "lock id:" << id << std::endl;
}; for (int i = 0; i < 10; ++i) {
std::thread run(fn_worker_thread,i);
run.detach();
} //cv.notify_one();
cv.notify_all(); getchar();
return 0;
}

线程调用类成员函数,需要显示的传递成员函数默认传递的 this 指针,即当前实例化对象指针,后面再传递你需要的参数。

class AsyncTest{
public:
void print(){
fprintf(stderr,"print\r\n");
}
int calc(int x,int y,int &result){
result = x + y;
return result;
}
}; int main()
{
AsyncTest test; {// call function void print()
std::future<void> async = std::async(std::launch::async,&AsyncTest::print,&test);
async.get();
} {// call function int calc(int x,int y,int &result)
int result = 0;
int x = 3;
int y = 4;
std::future<int> async = std::async(std::launch::async,&AsyncTest::calc,&test,x,y,std::ref(result));
int output = async.get();
fprintf(stderr,"x + y = %d output = %d",result,output);
} return 0;
}

c++ 如何获取多线程的返回值?的更多相关文章

  1. c++ 如何获取多线程的返回值?(std::thread ,std::async)

    //简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...

  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. 利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程

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

  9. python asyncio 获取协程返回值和使用callback

    1. 获取协程返回值,实质就是future中的task import asyncioimport timeasync def get_html(url): print("start get ...

随机推荐

  1. maven系列--maven常用命令

    下一篇博客我会讲解用eclipse的m2插件来使用maven,这里先大概的了解下maven常用的命令.之后我在详细整理maven的生命周期,到时候会细致的讲解下这些指令应该要怎么使,maven都帮我们 ...

  2. Servlet开篇

    Servlet开篇 前面我已经说过好多遍了,如何学习好一个东西其实就是2个问题: 1,这个东西是干嘛的?为什么要玩这个东西? 2,怎么样就玩好这个东西了?具体的应该玩这个东西的什么? 其实现在对于我来 ...

  3. JavaWeb项目架构之Redis分布式日志队列

    架构.分布式.日志队列,标题自己都看着唬人,其实就是一个日志收集的功能,只不过中间加了一个Redis做消息队列罢了. 前言 为什么需要消息队列? 当系统中出现"生产"和" ...

  4. [ASP.NET][Session] 使用 SQLServer 会话管理解决 Session 丢失问题

    使用 SQLServer 会话管理解决 Session 丢失问题 步骤 1.通过命令行执行 aspnet_regsql.exe 程序(不要双击安装),先在 CMD 中输入命令 cd C:\Window ...

  5. 一步一步从原理跟我学邮件收取及发送 10.四句代码说清base64

    经过前几篇的文章,大家应该都能预感到一定要讲解 base64 函数的内容了.是的,马上要到程序登录的代码,base64 是必须要实现的. base64 很早以前我就接触了,在项目中也很喜欢用.但每换一 ...

  6. selenium的使用技巧及集成到scrapy

    为了爬取拉钩,今天学习了selenum的使用技巧.   from scrapy.http import HtmlResponse   class JSPageMiddleware(object):   ...

  7. linux下安装phpunit简单方法

    现在安装phpunit相当简单,只需要下载phar压缩格式的phpunit文件,给个执行权限,就可以执行了 以下是一段官方安装文档 wget https://phar.phpunit.de/phpun ...

  8. ABP官方文档翻译 3.7 领域事件(事件总线)

    领域事件(事件总线) 事件总线 注入IEventBus 获取默认实例 定义事件 预定义事件 处理异常 实体更改 触发事件 处理事件 处理基础事件 处理者异常 处理多个事件 注册处理者 自动 手动 取消 ...

  9. BZOJ 1116: [POI2008]CLO [连通分量]

    Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个town都有且只有一个入度 ...

  10. BZOJ 1069: [SCOI2007]最大土地面积 [旋转卡壳]

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2978  Solved: 1173[Submit][Sta ...