*:如果 std::async 中传递参数 std::lunnch::deferred ,就需要等待调用 get() 或者 wait() 才会执行,并且代码非子线程运行,而是在主线程中执行
#include <iostream>
#include <thread>
#include <mutex>
#include <list>
#include <future>
using namespace std; int myThread()
{
cout << "myThread() start thread id =" << this_thread::get_id() << endl;
std::chrono::microseconds s(5000);
std::this_thread::sleep_for(s);
cout << "myThread() end thread id =" << this_thread::get_id() << endl;
return 5;
} class A
{
public:
int myThread(int num)
{
cout << num << endl;
cout << "myThread() start thread id =" << this_thread::get_id() << endl;
std::chrono::microseconds s(5000);
std::this_thread::sleep_for(s);
cout << "myThread() end thread id =" << this_thread::get_id() << endl;
return 10;
}
}; int main()
{
cout << "main run thread id =" << std::this_thread::get_id() << endl;
// 创建一个异步线程,使用函数作为参数
std::future<int> res = std::async(myThread);
// 创建一个异步线程,使用类作为参数
A a;
std::future<int> res1 = std::async(&A::myThread, &a, 1234);
cout << "continue ....." << endl;
// 获取线程返回的值
cout << res.get() << endl;
cout << res1.get() << endl;
cout << "main end" << endl;
return 0;
}

std::thread 四:异步(async)的更多相关文章

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

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

  2. 同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式

    1. 概念理解        在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:   同步/异步主要针对C端: 同步:    ...

  3. C++11并发——多线程std::thread (一)

    https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...

  4. C++ std::thread

    std::thread Defined in header class thread The class thread represents a single thread of execution. ...

  5. C++并发低级接口:std::thread和std::promise

    std::thread和std::promise 相比std::async,std::thread就原始多了.thread一定会创建新线程(而不是像async那样创建的时候可能不会,后面才创建新线程( ...

  6. Python 进阶 异步async/await

    一,前言 本文将会讲述Python 3.5之后出现的async/await的使用方法,我从上看到一篇不错的博客,自己对其进行了梳理.该文章原地址https://www.cnblogs.com/dhcn ...

  7. 关于std::thread

    std::thread基本用法 1.普通函数: std::thread thread(func, param, ...) 2.类成员函数: std::thread thread(&class_ ...

  8. C++11并发之std::thread<转>

    最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...

  9. C++11并发编程:多线程std::thread

    一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...

  10. C++11多线程std::thread创建方式

    //#include <cstdlib> //#include <cstdio> //#include <cstring> #include <string& ...

随机推荐

  1. 【Azure 存储服务】Storage Account Blob 使用REST API如何获取磁盘大小(Content-Length), IOPS信息

    问题描述 1)关于使用Rest API获取非托管磁盘信息比如获取磁盘大小 2)关于使用Rest API获取非托管磁盘信息比如iops 问题答案 #1:关于使用Rest API获取非托管磁盘信息比如获取 ...

  2. 2023 年值得一读的技术文章 | NebulaGraph 技术社区

    在之前的产品篇,我们了解到了 NebulaGraph 内核及周边工具在 2023 年经历了什么样的变化.伴随着这些特性的变更和上线,在[文章]博客分类中,一篇篇的博文记录下了这些功能背后的设计思考和研 ...

  3. C#系列文章索引

    由于有读者说,是否可以讲C#一类的文章都统一在一起,因此我做了个索引文章置顶起来,以后C#相关文章也会同步更新到该索引文章下,以便查找 .Neter所应该彻底了解的委托 - RyzenAdorer - ...

  4. Jmeter json断言的使用

    1 添加方式:取样器右键->添加->断言->JSON断言 作用:使用JSON表达式提取实际数据与预期进行比较   2首先我们来了解下断言组件的各个功能: Asset JSON Pat ...

  5. Java对象引用和内存管理的细节

    在Java中,当局部变量(比如方法参数)的作用域结束时,这个局部变量的引用确实不再存在,但这并不意味着它引用的对象会被销毁.对象的销毁是由Java的垃圾回收器(Garbage Collector, G ...

  6. [非常重要] 通过ssh的方式提交github

    通过ssh的方式提交github - 重要文章!!vscode提交github 原因: github的https的clone项目报错,所以改用ssh的方式 1 本地创建ssh秘钥 目录是 .ssh 我 ...

  7. reciterdoc 资料库 支持中文搜索了。 vuepress-plugin-fulltext-search(用一半)

    http://pengchenggang.gitee.io/reciterdoc/ 支持中文搜索了,可是不容易了 通过插件 vuepress-plugin-fulltext-search 实现全文搜索 ...

  8. css class 操作列 按钮 之间加 竖线 | class="your-handleBtn" :last-child::after

    思路:botton 加一个class 每个右边加个竖线,最后一个不加. 用起来很方便 <template slot="handle" slot-scope="{ r ...

  9. ECharts 中国地图 vue

    <template> <div> <div id="china_map_box"> <div id="china_map&quo ...

  10. 基于BES2500芯片的低功耗蓝牙BLE游戏手柄解决方案源码解析

    一 往事    寒冬腊月,在一个寂静的天空飘着碎银雪花的夜晚.我接到这么一个电话:"朋友,能否帮忙开发一个游戏手柄的案子?我们遇到了一些问题,迟迟无法解决.",喔,这边我陷入了沉思 ...