*:如果 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. CoaXPress 协议的CRC及其具体实现

    CoaXPress CRC 在CXP协议中,CRC用在stream packet和control packet中,用于指示数据是否错误,如果是control packet, device发现CRC错误 ...

  2. sql组合索引怎样使用?怎样命中?

    一.联合索引的使用 本文中联合索引的定义为(MySQL): ALTER TABLE table_name ADD INDEX (col1,col2,col3); 二.联合索引的本质 当创建(col1, ...

  3. 四种色彩模式ARGB_8888、ARGB_4444、 RGB_565、 ALPHA_8

    A:透明度. R:红色. G:绿色. B:蓝色. Bitmap.Config ARGB_8888:有四个8位组成,A,R,G,B各占八位,也就是各占一个字节.也就是一个像素点占4个字节,32位. Bi ...

  4. Java 演示线程的死锁问题

    1 package bytezero.deadlock; 2 3 /** 4 * 演示线程的死锁问题: 5 * 6 * 1.死锁的理解:不同的线程分别占用对方需要的同步资源不放弃,都在等待对方放弃 7 ...

  5. 使用jenkins连接linux部署jar包

    jenkins安装 首先安装jenkins,我们可以使用docker安装.用下面命令拉取jenkins镜像. docker pull jenkins/jenkins 然后正常安装jenkins容器即可 ...

  6. map 简单梳理【GO 基础】

    〇.map 简介 map 是一种无序的基于 key-value 的数据结构,Go 语言中的 map 是引用类型,必须初始化才能使用. 其中键可以是任何类型,但值必须是可比较的类型(如整数.字符串.布尔 ...

  7. 基于ESP8266的JSON解析实例分析

    什么是JSON?   JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.采用完全独立于编程语言的文本格式来存储和表示数据.其简洁和层次结构清晰的特点使得 J ...

  8. C# 中的for/foreach循环

    for 循环是一个执行特定次数的循环的重复控制结构. C# 中 for 循环的语法: for ( init; condition; increment ) { statement(s); } 执行流程 ...

  9. [置顶] 彻底停止运行线程池ThreadPoolExecutor

    最近系统开发时遇到这样一个需求: 该功能执行时间很久,如果运行过程出现错误,也无法将其停止,必须眼睁睁的看着它浪费很久时间,除非停止服务器. 于是,我就想着如何给该功能加上一个"停止&quo ...

  10. Android13源码下载环境搭建

    由于AOSP的下载&编译等工作,需要用到git与python的支持,所以需要提前安 装好 VMware解决Ubuntu不占满全屏问题和Windows复制粘贴问题 https://blog.cs ...