*:如果 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. STM32FATFS文件系统移植

    STM32FATFS文件系统移植 1. FATFS简介 FATFS文件系统是一个用于在微控制器上运行的开源文件系统,支持FAT/FATFS.NTFS.exFAT等主流文件系统,且一直保持更新.在此以F ...

  2. shell脚本的基本使用

    本文是对菜鸟教程 shell编程原文 的总结并记录 如有侵权 联系删除 简介 说明: Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.这个应用程序提供了一个界面,用户通过这 ...

  3. Java 常用类 于 StringBuffer 和 StringBuilder的使用 + String三者的异同

    1 package com.bytezero.stringclass; 2 3 import org.junit.Test; 4 5 /** 6 * 关于 StringBuffer 和 StringB ...

  4. Java 从键盘输入不确定的整数 并判断读入的整数和负数的个数,输入0时候结束

    1 /** 2 * 从键盘输入不确定的整数 并判断读入的整数和负数的个数,输入0时候结束 3 * 4 */ 5 6 Scanner scan = new Scanner(System.in); 7 8 ...

  5. windows编译ZLMediaKit流媒体服务webrtc

    环境说明 ZLMediaKit编译需要的软件 visual studio 2022 cmake 3.29.0-rc2 OpenSSL 1.1.1w(不想踩坑的话安装这个版本) libsrtp 2.6. ...

  6. gRPC入门学习之旅(一)

    gRpc简介 gRPC 是Google公司开发的基于HTTP/2设计,面向移动的一个高性能.开源和通用的 RPC 框架,是一款语言中立.平台中立.开源的远程过程调用(RPC)系统. gRpc官网地址: ...

  7. jQuery 框架

    jQuery 框架 目录 jQuery 框架 一. 概述 二. jQuery 安装引用 2.1 安装 2.2 本地导入使用 2.3 jQuery CDN引入 三. jQuery基本语法 四. 查找标签 ...

  8. 浅谈Zabbix与Prometheus区别

    Zabbix和Prometheus都是非常流行的监控系统.它们有许多相似之处,但也有一些不同之处.以下是Zabbix和Prometheus监控对比的一些关键点: 1.数据模型和查询语言 Prometh ...

  9. 从零开始写 Docker(五)---基于 overlayfs 实现写操作隔离

    本文为从零开始写 Docker 系列第五篇,在 pivotRoot 基础上通过 overlayfs 实现写操作隔离,达到容器中写操作和宿主机互不影响. 完整代码见:https://github.com ...

  10. vue css 背景图 路径 记得加波浪号 background-image: url("~@/assets/images/d.jpg");

    vue css 背景图 路径 记得加波浪号 background-image: url("~@/assets/images/d.jpg"); 为什么 不加波浪号,图片显示不出来哟