C++11多线程:std::thread创建线程和std::async创建异步任务的区别,std::async创建异步任务后没有被推迟执行。

news/2025/2/16 6:59:17/

系列文章目录


文章目录

  • 系列文章目录
  • 前言
  • 一、thread和async的区别
    • 1.1 新线程和异步任务
    • 1.2 std::async和std::thread最明显的不同,就是async有时候并不创建新线程。
    • 1.3 std::async和std::thread的区别
    • 1.4 std::async不确定性问题的解决
  • 二、使用方法
    • 2.1 std::async创建"异步任务"
    • 2.2 std::async不确定性问题的解决测试代码
  • 总结


前言

(1) thread创建新的线程;
(2) async创建新的异步任务;
有没有创建新线程立即执行还是延迟(没创建新线程)执行
(3)std::thread和std::async的区别;


一、thread和async的区别

std::async创建异步任务测试代码:本章节2.1
std::async不确定性问题的解决测试代码:本章节2.2

1.1 新线程和异步任务

  • std::thread() 如果系统资源紧张,那么可能创建线程就会失败,那么执行std::thread()时整个程序可能会崩溃。
  • std::async()我们一般不叫创建线程(解释async能够创建线程),我们一般叫它创建 一个异步任务。

1.2 std::async和std::thread最明显的不同,就是async有时候并不创建新线程。

  • a)如果你用std::launch::deferred来调用async会怎么样?

    std::launch::deferred延迟调用,并且不创建新线程,延迟到future对象调用.get()或者.wait()的时候才执行mythread(),如果没有调用get或者wait,那么这个mythread就不会执行。

  • b)std::launch::async:强制这个异步任务在新线程上执行,这意味着,系统必须要给我创建出新线程来运行mythread();

  • c)std::launch::async | std::launch::deferred 这里这个|:意味着调用async的行为可能是
    “创建新线程并立即执行” 或者 “没有创建新线程并且延迟到调用 result.get()才开始执行任务入口函数, 两者居其一”

  • d)我们不带额外参数;只给一个async函数一个 入口函数名;
    换句话说:系统会自行决定是异步(创建新线程)还是同步(不创建新线程)方式运行。自行决定是啥意思?系统如何决定是
    异步(创建新线程)还是同步(不创建新线程)方式运行。

1.3 std::async和std::thread的区别

std::thread创建线程,如果系统资源紧张,创建线程失败,那么整个程序就会报异常崩溃(有脾气)

    int mythread(){return 1;}std::thread mytobj(mythread);mytobj.join();

std::thread创建线程的方式,如果线程返回值,你想拿到这个值也不容易;
std::async创建异步任务。可能创建也可能不创建线程。并且async调用方法很容易拿到线程入口函数的返回值;

由于系统资源限制:

  1. 如果用std::thread创建的线程太多,则可能创建失败,系统报告异常,崩溃。
  2. 如果用std::async,一般就不会报异常不会崩溃,因为,如果系统资源紧张导致无法创建新线程的时候,std::async这种不加额外参数的调用就不会创建新线程。而是后续谁调用了result.get()来请求结果,那么这个异步任务mythread就运行在执行这条get()所在的线程上。

如果你强制std::async一定 要创建新线程,那么就必须使用
std::launch::async。承受的代价就是系统资源紧张时,程序崩溃。

  1. 经验:一个程序,线程数量不宜超过100-200,时间片。

1.4 std::async不确定性问题的解决

有没有创建新线程立即执行还是延迟(没创建新线程)执行?

测试方法:
不加额外参数的std::async调用,让系统自行决定是否创建新线程。
问题焦点在于 std::future result = std::async(mythread);写法
这个异步任务到底有没有被推迟执行,(std::launch::async还是std::launch::deferred)
std::future对象的wait_for函数。

代码参考2.2章节

二、使用方法

2.1 std::async创建"异步任务"

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <list>
#include <mutex>
#include <future>
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。#include <SDKDDKVer.h>
int mythread2()
{cout << "mythread start" << " threadid = " << std::this_thread::get_id() << endl;return 1;
}int main()
{//二:std::async深入谈//(2.1)std::async参数详述,async用来创建一个异步任务cout << "main start" << " threadid = " << std::this_thread::get_id() << endl;//std::future<int> result = std::async(std::launch::deferred, mythread2);	//deferred延迟调用,并且不创建新线程,延迟到future对象调用.get()或者.wait()的时候才执行mythread()//std::future<int> result = std::async(std::launch::async, mythread2);//std::future<int> result = std::async(std::launch::async | std::launch::deferred, mythread2);std::future<int> result = std::async(std::launch::async | std::launch::deferred, mythread2);cout << result.get() << endl;return 0;
}

2.2 std::async不确定性问题的解决测试代码

代码如下(示例):

#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <map>
#include <string>
#include <thread>
#include <list>
#include <mutex>
#include <future>
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。#include <SDKDDKVer.h>int mythread3()
{cout << "mythread start" << " threadid = " << std::this_thread::get_id() << endl;std::chrono::milliseconds dura(5000);	//1秒 = 1000毫秒,所以5000毫秒 = 5秒std::this_thread::sleep_for(dura);		//休息一定的时长return 1;
}int main()
{cout << "main start" << " threadid = " << std::this_thread::get_id() << endl;std::future<int> result = std::async(mythread3);	//想判断async到底有没有创建新线程立即执行还是延迟(没创建新线程)执行。std::future_status status = result.wait_for(std::chrono::seconds(0));	if (status == std::future_status::deferred){//线程被延迟执行了(系统资源紧张了,它给我采用std::launch::deferred策略了)cout << result.get() << endl;	//这个时候才去调用了mythread();}else{//任务没有被推迟,已经开始运行了呗,线程被创建了;if (status == std::future_status::ready){//线程成功返回cout << "线程成功执行完毕并返回!" << endl;cout << result.get() << endl;}else if (status == std::future_status::timeout){//超时线程还没执行完cout << "超时线程还没执行完!" << endl;cout << result.get() << endl;}}return 0;
}

运行截图:
在这里插入图片描述

总结:
异步任务创建后,没有被推迟执行。


总结

(1)std::async的强制创建异步任务()与std::thread创建线程相似,容易系统崩溃。

std::async(std::launch::async, mythread2);

(2)std::async不带参数的创建异步任务,不会造成系统崩溃。

//默认自带的参数
std::async(std::launch::async | std::launch::deferred, mythread2);
std::async(mythread2);

(3)使用std::async不带参数创建的线程 ,异步任务创建后,没有被推迟执行。


http://www.ppmy.cn/news/63311.html

相关文章

动态规划--01背包问题

01背包问题 背包问题题目最优解结构性质状态转移方程方程理解 递归实现核心思想代码实现用例测试 画表非递归实现核心思路代码实现画表展示 计算哪些物品放入算法思想代码实现 背包问题 题目 0-1背包问题:给定n种物品和一背包。物品的重量是w;,其价值为v; ,背包的容量为C。问…

Android studio单独导入官方例程camera-calibration

1.官方例程camera-calibration 2.将官方例程camera-calibration copy到AndroidStudioProjects项目目录下 3修改AndroidManifest.xml <?xml version"1.0" encoding"utf-8"?> <manifest xmlns:android“http://schemas.android.com/apk/res/andr…

医院智能导诊系统,医院导航解决方案

随着现代医院规模不断扩大&#xff0c;功能区域越来越细化&#xff0c;面对复杂的楼宇结构&#xff0c;集中的就诊人流&#xff0c;患者在就诊中经常会面临找不到目的地的困境&#xff0c;就诊体验变差。针对这个问题&#xff0c;一些面积和规模都比较大的医院&#xff0c;已经…

零基础入门 Stable Diffusion - 无需显卡把 AI 绘画引擎搬进家用电脑

我从小特别羡慕会画画的伙伴。他们能够将心中的想法画出来&#xff0c;而我最高水平的肖像画是丁老头。但在接触 Stable Diffusion 之后&#xff0c;我感觉自己脱胎换骨&#xff0c;给自己贴上了「会画画」的新标签。 丁老头进化旅程 Stable Diffusion 是一个「文本到图像」的…

线上问题-CPU使用频率飙升

描述 中午收到群内人员反馈环境访问速度慢。登录验证码打不开等问题。通过查看日志发现是kafka出现问题&#xff0c;无法处理消息。联系运维解决。在排查的过程中使用mobaXterm连接服务器。左下角看到CPU使用频率非常高。于是记录一下通过CPU查看程序占用情况分析问题。 过程 …

JVM(类的加载与ClassLoader、双亲委派机制)

文章目录 1. 类的生命周期2. 类的加载过程3. 类加载器&#xff08;classloader)3.1 类加载器的作用3.2 类加载器的分类(JDK8)3.3 双亲委派机制3.3.1 双亲委派机制优势 3.4 查看某个类的类加载器对象3.5 使用ClassLoader获取流 1. 类的生命周期 类在内存中完整的生命周期&#…

MapReduce框架原理

从源码的角度 :map --> sort —> copy --> sort -->reduce   sort —> copy --> sort属于shuffle InputFormat数据输入 切片与MapTask并行度决定机制 1&#xff09;问题引出 MapTask的并行度决定Map阶段的任务处理并发度&#xff0c;进而影响到整个Job的…

如何安装oracle的sample schema

首先从如下的地址选择合适的版本进行下载 https://github.com/oracle-samples/db-sample-schemas/releases 如果是rac环境&#xff0c;最好是将这个数据库停掉&#xff0c;然后只启动一个instance&#xff0c;然后再开始安装 [Tue May 09 20:26:34][377951][oraclenshqae01adm…