当前位置: 首页 > news >正文

c++多线程中常用的使用方法

1)promise(保证)和future的联合使用,实现两个线程的数据传递

#include <iostream>
#include<thread>
#include<future>using namespace std;//promise用法:可以给线程一个值,而从另一个线程读出该值
// 实现了两个线程的数据传递!
void myPromise(std::promise<int>& tmp,int num)//promise线程
{//在这里假使处理复杂的计算,最后得出一个值,并返回num = num + 20;tmp.set_value(num);
}//另一个线程可以得到promise计算后得到的值
void myFuture(std::future<int>& tmp)
{cout<<"myFuture thread receive value is : "<< tmp.get()<<endl;
}int main()
{std::promise<int> pro;std::thread th1(myPromise,std::ref(pro),10); //创建promise线程时,第二个参数一定要用引用th1.join();std::future<int> result = pro.get_future();std::thread th2(myFuture,std::ref(result));  //std::ref( pro.get_future())用临时对象不可以;th2.join();return 0;
}

//输出结果:myFuture thread receive value is : 30
2) package_task的使用,把任务从新包装起来,案例代码如下:

#include <iostream>
#include<thread>
#include<future>using namespace std;int func(void)
{cout<<"start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::this_thread::sleep_for(std::chrono::milliseconds(2000));cout<<"end-------- thread_id is : "<<std::this_thread::get_id()<<endl;return 5;
}//package_task类模板,可以包装任何可调用对象
int main()
{std::packaged_task<int()> task(func);std::thread th(std::ref(task)); //package_task里面有一个成员函数get_future(),可以返回一个future对象;th.join();std::future<int> result = task.get_future();cout<<result.get()<<endl;return 0;
}

3)//std::async和std::future一起配合使用

#include <iostream>
#include<thread>
#include<mutex>
#include<list>
#include<future>// std::async和std::future的用法:
// async是一个函数模板,启动一个线程;std::future是类模板,当async启动任务后,会返回std::future对象;
// std::future对象里面有需要的返回值;
using namespace std;int func(void)
{cout<<"start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::this_thread::sleep_for(std::chrono::milliseconds(2000));cout<<"end-------- thread_id is : "<<std::this_thread::get_id()<<endl;return 5;
}//std::launch::deferred参数时,等待wait或者get返回,如果没有这两个函数,直接运行接下来代码
//std::launch::deferred参数不会创建新线程!!!
//std::future两个成员函数,get是等待线程处理完返回结果;wait是等待线程处理完,不返回结果
int main()
{cout<<"main thread start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::future<int> result = std::async(func); //程序不会卡到这里!!!这里存在一个绑定关系cout<<"********main run*********"<<endl;cout<<result.get()<<endl; //程序会卡到这里,等待线程返回结果(利用的是std::future的成员函数get)return 0;
}

4)//条件变量与互斥量的配合使用方法:

#include <iostream>
#include<thread>
#include<mutex>
#include<list>
#include<condition_variable>
//条件变量是和互斥量配合使用!using namespace std;class A{
public:void inQueue(void){for(int i = 0; i<10000;i++){cout<<"insert message : "<<i<<endl;{std::unique_lock<std::mutex> uniqueLock(mtx); message.push_back(i);cond.notify_one();}}}void outQueue(void){for(int i = 0 ; i<10000; i++){std::unique_lock<std::mutex> uniqueLock(mtx); //自动加锁//wait跟互斥量的关系:wait阻塞解锁,wait唤醒加锁!!!cond.wait(uniqueLock,[this](){ //如果参数2是false,解锁等待;如果是true,加锁完成后,继续执行下面代码if(!message.empty()) return true;elsereturn false; //可以处理虚假唤醒,就是说容器没有数据,但被唤醒了,执行解锁等待。});       int cmd = message.front();message.pop_front();             cout<<"out cmd---------- is :"<<cmd<<endl;     }//出作用域,uniqueLock锁释放;}
private:std::list<int> message;std::condition_variable cond;std::mutex mtx;};int main()
{A a;std::thread th1(&A::inQueue,&a);std::thread th2(&A::outQueue,&a);th1.join();th2.join();return 0;
}

5)互斥量的使用方法:

#include <iostream>
#include<thread>
#include<mutex>
#include<list>
#include<condition_variable>using namespace std;class A{
public:void inQueue(void){for(int i = 0; i<10000;i++){cout<<"insert message : "<<i<<endl;{std::unique_lock<std::mutex> uniqueLock(mtx); message.push_back(i);}std::this_thread::sleep_for(std::chrono::milliseconds(1));}}void outQueue(void){for(int i = 0; i<10000;i++){if(!message.empty()){int cmd = 0;{std::unique_lock<std::mutex> uniqueLock(mtx); cmd = message.front();message.pop_front();}cout<<"out cmd is :"<<cmd<<endl;}else{cout<<"empty---------------i: "<<i<<endl;}std::this_thread::sleep_for(std::chrono::milliseconds(1));}}
private:std::list<int> message;std::mutex mtx;};int main()
{A a;std::thread th1(&A::inQueue,&a);std::thread th2(&A::outQueue,&a);th1.join();th2.join();return 0;
}

6)单例模式在线程中的使用

#include <iostream>
#include<thread>
#include<mutex>using namespace std;std::mutex mtxRes;
//单例目的:即使多个线程被创建,也要保证只创建一个对象,所以创建多线程创建对象时,要做互斥量条件判断!!!
class A
{
private:A(){cout<<"A conctructor---"<<endl;}
private:static A* m_instance; //声明一个静态私有类指针变量
public:static A* getInstance(){if(m_instance == nullptr) //多个线程,为了提升速度(双重锁定){std::unique_lock<std::mutex> uniquelock(mtxRes);if(m_instance == nullptr){m_instance = new A();static garb cl;//为了程序退出后,能够正常删除m_instance}}return m_instance;}void test(void){cout<<"test------"<<endl;}class garb //为了能正常delete m_instance,而设计的内部类{public:~garb(){if(A::m_instance != nullptr){delete A::m_instance;A::m_instance = nullptr;}}};
};A* A::m_instance = nullptr;void func(void)
{cout<<"-----开始单例模式创建--------"<<endl;A::getInstance()->test();cout<<"-----单例模式创建完成--------"<<endl;
}int main()
{// A::getInstance()->test();std::thread th1(func);std::thread th2(func);        th1.join();th2.join();return 0;
}

相关文章:

c++多线程中常用的使用方法

1)promise(保证)和future的联合使用&#xff0c;实现两个线程的数据传递 #include <iostream> #include<thread> #include<future>using namespace std;//promise用法&#xff1a;可以给线程一个值&#xff0c;而从另一个线程读出该值 // 实现了两个线程的数…...

【dart】dart基础学习使用(一):变量、操作符、注释和库操作

前言 学习dart语言。 注释 Dart 支持单行注释、多行注释和文档注释。 单行注释 单行注释以 // 开头。Dart 编译器将忽略从 // 到行尾之间的所有内容。 void main() {// 这是单行注释print(Welcome to my Llama farm!); }多行注释 多行注释以 /* 开始&#xff0c;以 / 结…...

element-plus 设置 el-date-picker 弹出框位置

前言 概述&#xff1a;el-date-picker 组件会自动根据空间范围进行选择比较好的弹出位置&#xff0c;但特定情况下&#xff0c;它自动计算出的弹出位置并不符合我们的实际需求&#xff0c;故需要我们手动设置。 存在的问题&#xff1a;element-plus 中 el-date-picker 文档中并…...

C++day7(auto关键字、lambda表达式、C++中的数据类型转换、C++标准模板库(STL)、list、文件操作)

一、Xmind整理&#xff1a; 关键词总结&#xff1a; 二、上课笔记整理&#xff1a; 1.auto关键字 #include <iostream>using namespace std;int fun(int a, int b, float *c, char d, double *e,int f) {return 12; }int main() {//定义一个函数指针&#xff0c;指向fu…...

纽扣电池/锂电池UN38.3安全检测报告

根据规章要求&#xff0c;航空公司和机场货物收运部门应对锂电池进行运输文件审查&#xff0c;重要的是每种型号的锂电池UN38.3安全检测报告。该报告可由的三方检测机构。如不能提供此项检测报告&#xff0c;将禁止锂电池进行航空运输. UN38.3包含产品&#xff1a;1、 锂电池2…...

K8S:K8S自动化运维容器Docker集群

文章目录 一.k8s概述1.k8s是什么2.为什么要用K8S3.作用及功能4.k8s容器集群管理系统 二.K8S的特性1.弹性伸缩2.自我修复3.服务发现和复制均衡4.自动发布和回滚5.集中化配置管理和秘钥管理6.存储编排7.任务批量处理运行 三.K8S的集群架构四.K8S的核心组件1.Master组件&#xff0…...

Java的guava 限流写法

第一步先引入 maven <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.0.1-jre</version> </dependency> 然后上方法 private final double rateLimiter10 1.0 / 10.0; // 每…...

[uniapp] scroll-view 简单实现 u-tabbar效果

文章目录 方案踩坑1.scroll-view 横向失败2.点击item不滚动?3. scrollLeft从哪里来? 效果图 方案 官方scroll-view 进行封装 配合属性 scroll-left Number/String 设置横向滚动条位置 即可 scroll-into-view 属性尝试过,方案较难实现 踩坑 1.scroll-view 横向失败 安装…...

vue常见问题汇总

来源&#xff1a;https://www.fly63.com/ Q1&#xff1a;安装超时(install timeout) 方案有这么些: cnpm : 国内对npm的镜像版本/*cnpm website: https://npm.taobao.org/*/npm install -g cnpm --registryhttps://registry.npm.taobao.org// cnpm 的大多命令跟 npm 的是一致的…...

GPT-3在化学中进行低数据发现是否足够?

今天介绍一份洛桑联邦理工学院进行的工作&#xff0c;这份工作被发表在化学期刊预印本网站上。 对于这份工作&#xff0c;有兴趣的朋友可以通过我们的国内ChatGPT镜像站进行测试使用&#xff0c;我们的站点并没有针对特定任务进行建设&#xff0c;是通用性质的。 化学领域进行…...

gitlab升级

1.下载需要的版本 wget -c https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.7.6-ce.0.el7.x86_64.rpm --no-check-certificate gitlab-ce-15.4.6-ce.0.el7.x86_64.rpm gitlab-ce-15.7.6-ce.0.el7.x86_64.rpm gitlab-ce-15.9.7-ce.0.el7.x86_64.rpm g…...

Matlab图像处理-灰度插值法

最近邻法 最近邻法是一种最简单的插值算法&#xff0c;输出像素的值为输入图像中与其最邻近的采样点的像素值。是将(u0,v0)(u_0,v_0)点最近的整数坐标u,v(u,v)点的灰度值取为(u0,v0)(u_0,v_0)点的灰度值。 在(u0,v0)(u_0,v_0)点各相邻像素间灰度变化较小时&#xff0c;这种方…...

axios 或 fetch 如何实现对发出的请求的终止?

终止 HTTP 请求是一个重要的功能&#xff0c;特别是在需要优化性能、避免不必要的请求或在某些事件发生时&#xff08;例如用户点击取消&#xff09;中断正在进行的请求时。以下是如何使用 axios 和 fetch 实现请求终止的方法&#xff1a; 1. axios axios 使用了 CancelToken…...

ChatGPT Prompting开发实战(四)

一、chaining prompts应用解析及输出文本的设定 由于输入和输出都是字符串形式的自然语言&#xff0c;为了方便输入和输出信息与系统设定使用的JSON格式之间进行转换&#xff0c;接下来定义从输入字符串转为JSON list的方法&#xff1a; 定义从JSON list转为输出字符串的方法&…...

Windows和Linux环境中安装Zookeeper具体操作

1.Windows环境中安装Zookeeper 1.1 下载Zookeeper安装包 ZooKeeper官网下载地址 建议下载稳定版本的 下载后进行解压后得到如下文件&#xff1a; 1.2 修改本地配置文件 进入解压后的目录&#xff0c;将zoo_example.cfg复制一份并重命名为zoo.cfg,如图所示&#xff1a; 打…...

41、Flink之Hive 方言介绍及详细示例

Flink 系列文章 1、Flink 部署、概念介绍、source、transformation、sink使用示例、四大基石介绍和示例等系列综合文章链接 13、Flink 的table api与sql的基本概念、通用api介绍及入门示例 14、Flink 的table api与sql之数据类型: 内置数据类型以及它们的属性 15、Flink 的ta…...

docker环境安装软件、更换镜像源以及E: Unable to locate package xxx解决

docker环境安装vim、ifconfig、ping、更换镜像源以及E: Unable to locate package vim 一. E: Unable to locate package vim 问题解决一、问题分析二、解决方案三、再次安装四. 此镜像源已失效 二. 解决 “E: 仓库xx没有 Release 文件。N: 无法安全地用该源进行更新&#xff0…...

夸克扫描王App用上了AI大模型 让扫描更清楚、提取文字更方便

对上班族来说&#xff0c;找到一个好用的工具类APP&#xff0c;绝对可以提升工作效率。比如最常见的扫描文件&#xff0c;公司的扫描仪虽然好用但是很难进行深度编辑且不能外出使用&#xff1b;很多手机App也有扫描功能&#xff0c;但技术能力总是差一点&#xff0c;当面对复杂…...

代价高昂的 IT 错误:识别并避免供应商锁定

陷入不提供所需服务的云服务器合同中可能会非常痛苦、令人沮丧且成本高昂。 供应商锁定是提供商难以切换的地方&#xff0c;这意味着企业迁移到新供应商的成本太高、破坏性太大或耗时。 这使得公司受到供应商的摆布&#xff0c;尽管该服务可能无法提供他们所需的可靠性或可扩…...

HBase集群环境搭建与测试

&#x1f947;&#x1f947;【大数据学习记录篇】-持续更新中~&#x1f947;&#x1f947; 个人主页&#xff1a;beixi 本文章收录于专栏&#xff08;点击传送&#xff09;&#xff1a;【大数据学习】 &#x1f493;&#x1f493;持续更新中&#xff0c;感谢各位前辈朋友们支持…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)

HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...

DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径

目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(九)

设备树移植 和uboot设备树修改的内容同步到kernel将设备树stm32mp157d-stm32mp157daa1-mx.dts复制到内核源码目录下 源码修改及编译 修改arch/arm/boot/dts/st/Makefile&#xff0c;新增设备树编译 stm32mp157f-ev1-m4-examples.dtb \stm32mp157d-stm32mp157daa1-mx.dtb修改…...

图表类系列各种样式PPT模版分享

图标图表系列PPT模版&#xff0c;柱状图PPT模版&#xff0c;线状图PPT模版&#xff0c;折线图PPT模版&#xff0c;饼状图PPT模版&#xff0c;雷达图PPT模版&#xff0c;树状图PPT模版 图表类系列各种样式PPT模版分享&#xff1a;图表系列PPT模板https://pan.quark.cn/s/20d40aa…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”

2025年#高考 将在近日拉开帷幕&#xff0c;#AI 监考一度冲上热搜。当AI深度融入高考&#xff0c;#时间同步 不再是辅助功能&#xff0c;而是决定AI监考系统成败的“生命线”。 AI亮相2025高考&#xff0c;40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕&#xff0c;江西、…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...

elementUI点击浏览table所选行数据查看文档

项目场景&#xff1a; table按照要求特定的数据变成按钮可以点击 解决方案&#xff1a; <el-table-columnprop"mlname"label"名称"align"center"width"180"><template slot-scope"scope"><el-buttonv-if&qu…...