*:如果 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. SwitUI初次体验

    序言 开年的第一篇文章,今天分享的是SwiftUI,SwiftUI出来好几年,之前一直没学习,所以现在才开始:如果大家还留在 iOS 开发,这们语言也是一个趋势: 目前待业中.... 不得不说已逝的2 ...

  2. 第一百零一篇:DOM节点类型

    好家伙,   DOM DOM是javascript操作网页的接口,全称为文档对象模型(Document Object Model).它的作用是将网页转为一个javascript对象, 从而可以使用ja ...

  3. 【Azure APIM】解决APIM Self-hosted Gateway在AKS上,最开始访问时候遇见的404问题

    问题描述 根据APIM官方文档,创建Self-hosted 网关在AKS中( 使用 YAML 将自承载网关部署到 Kubernetes :https://docs.azure.cn/zh-cn/api ...

  4. 如何使用 perf 分析 splice 中 pipe 的容量变化

    如何使用 perf 分析 splice 中 pipe 的容量变化 这个文章为了填上一篇文章的坑的,跟踪内核函数本来是准备使用 ebpf 的,但是涉及到了低内核版本,只能使用 kprobe 了. 恰好, ...

  5. [vbs] 定时关闭进程代码

    Dim bag,pipe do Set bag=GetObject("WinMgmts:") Set pipe=bag.execquery("select * from ...

  6. Docker:Failed to copy files, no space left on device

    主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ 问题描述 在Mac上进行docker构建时,偶尔会遇到以下问题 Fai ...

  7. Nginx 打不开 80端口占用 netstat -aon|findstr "80" 看有没有80占用 有的话 net stop http

    Nginx 打不开 80端口占用 netstat -aon|findstr "80" 看有没有80占用 有的话 net stop http

  8. ubuntu添加桌面快捷打开方式

    不太喜欢ubuntu开机后空荡荡的桌面,希望可以有些像windows一样的快捷打开方式.看了一些博客,也自己探索了一下,发现了在ubuntu中添加软件自带的桌面快捷打开方式的方法. 在终端 cd /u ...

  9. Python | Flask 解决跨域问题

    Python | Flask 解决跨域问题 系列文章目录 目录 系列文章目录 前言 使用步骤 1. 引入库 2. 配置 1. 使用 CORS函数 配置全局路由 2. 使用 @cross_origin ...

  10. Android Progressbar进度条样式调整为圆角矩形,且改变颜色

    原文地址: Android Progressbar进度条样式调整为圆角矩形,且改变颜色 美工设计的进度条是圆角矩形的,与Android默认的样式有所区别,可以通过样式progressDrawable属 ...