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

C++多线程学习(一):C++11 多线程快速入门

参考引用

  • C++11 14 17 20 多线程从原理到线程池实战
  • 代码运行环境:Visual Studio 2019

1. 为什么要用多线程

  • 任务分解
    • 耗时的操作,任务分解,实时响应
  • 数据分解
    • 充分利用多核CPU处理数据
  • 数据流分解
    • 读写分离,解耦合设计

2. 第一个子线程代码示例

  • first_thread.cpp
    #include <iostream>
    #include <thread>using namespace std;// 创建的子线程的入口函数
    void ThreadMain() {// 获取子线程开始时刻的 ID 号cout << "begin sub thread ID " << this_thread::get_id() << endl;for (int i = 0; i < 5; i++) {cout << "in thread " << i << endl;// 子线程睡眠(释放)CPU 资源 1000msthis_thread::sleep_for(chrono::seconds(1));  }cout << "end sub thread ID " << this_thread::get_id() << endl;
    }// 主线程的入口函数 main()
    int main(int argc, char* argv[]) {cout << "main thread ID " << this_thread::get_id() << endl;// 线程创建启动thread th(ThreadMain);cout << "begin wait sub thread" << endl;// 阻塞等待子线程退出th.join();cout << "end wait sub thread" << endl;return 0;
    }
    
  • 控制台输出
    main thread ID 21580
    begin wait sub thread
    begin sub thread ID 22924
    in thread 0
    in thread 1
    in thread 2
    in thread 3
    in thread 4
    end sub thread ID 22924
    end wait sub thread
    

3. std::thread 对象生命周期、线程等待和分离

  • thread_detach.cpp

    #include <iostream>
    #include <thread>using namespace std;bool is_exit = false;void ThreadMain() {cout << "begin sub thread ID " << this_thread::get_id() << endl;for (int i = 0; i < 5; i++) {if (!is_exit)break;cout << "in thread " << i << endl;this_thread::sleep_for(chrono::seconds(1));  // 子线程睡眠(释放)CPU 资源 1000ms}cout << "end sub thread ID " << this_thread::get_id() << endl;
    }int main(int argc, char* argv[]) {{//thread th(ThreadMain); // 出错,thread 对象被销毁 子线程还在运行}{thread th(ThreadMain);th.detach();  // 子线程与主线程分离 守护线程(在后台运行)// 但存在一个问题:主线程退出后 子线程不一定退出}{thread th(ThreadMain);this_thread::sleep_for(chrono::seconds(1));//1000msis_exit = true;  // 通知子线程退出cout << "主线程阻塞,等待子线程退出" << endl;th.join();       // 主线程阻塞,等待子线程退出cout << "子线程已经退出!" << endl;}getchar();return 0;
    }
    
  • 控制台输出

    begin sub thread ID 25520
    end sub thread ID 25520
    begin sub thread ID 23324
    end sub thread ID 23324
    主线程阻塞,等待子线程退出
    子线程已经退出!
    

4. 全局函数作为线程入口

  • thread_para.cpp

    #include <iostream>
    #include <thread>
    #include <string>using namespace std;class Para {
    public:Para() {cout << "Create Para" << endl;}Para(const Para& p) {  // 拷贝构造函数cout << "Copy Para" << endl; }~Para() {cout << "Drop Para" << endl;}string name;
    };void ThreadMain(int p1, float p2, string str, Para p4) {this_thread::sleep_for(100ms);cout << "ThreadMain " << p1 << " " << p2 << " " << str << " " << p4.name << endl;
    }int main(int argc, char* argv[]) {thread th;{float f1 = 12.1f;Para p;p.name = "test Para class";// 所有的参数做复制th = thread(ThreadMain, 101, f1, "test string para", p);}th.join();return 0;
    }
    
  • 控制台输出

    Create Para
    Copy Para
    Drop Para
    Copy Para
    ThreadMain 101 12.1 test string para
    Drop Para
    Drop Para
    

5. 线程函数传递指针和引用

  • 参数传递存在的问题

    • 传递空间已经销毁
    • 多线程共享访问一块空间
    • 传递的指针变量的生命周期小于线程
  • thread_para.cpp

#include <thread>
#include <iostream>
#include <string>using namespace std;class Para {
public:Para() { cout << "Create Para" << endl; }Para(const Para& p) { cout << "Copy Para" << endl; }~Para() { cout << "Drop Para" << endl; }string name;
};void ThreadMain(int p1, float p2, string str, Para p4) {this_thread::sleep_for(100ms);cout << "ThreadMain " << p1 << " " << p2 << " " << str <<" "<<p4.name<< endl;
}void ThreadMainPtr(Para* p) {this_thread::sleep_for(100ms);cout << "ThreadMainPtr name = " << p->name << endl;
}void ThreadMainRef(Para& p) {this_thread::sleep_for(100ms);cout << "ThreadMainPtr name = " << p.name << endl;
}
int main(int argc, char* argv[]) {{// 传递引用Para p;p.name = "test ref";thread th(ThreadMainRef, ref(p));th.join();}getchar();{// 传递线程指针Para p;p.name = "test ThreadMainPtr name";thread th(ThreadMainPtr, &p);  th.detach();  // 错误,线程访问的 p 空间会提前释放}getchar();  // Para 释放{  // 传递线程指针Para p;p.name = "test ThreadMainPtr name";thread th(ThreadMainPtr, &p);th.join();getchar();}thread th;{float f1 = 12.1f;Para p;p.name = "test Para class";// 所有的参数做复制th =  thread(ThreadMain, 101, f1, "test string para", p);}th.join();return 0;
}
  • 控制台输出
    Create Para
    ThreadMainPtr name = test ref
    Drop Para
    

6. 成员函数作为线程入口–封装线程基类接口

  • XThread.h

    • 封装线程基类接口
    #pragma once#include <iostream>
    #include <thread>class XThread {
    public:virtual void Start() {is_exit_ = false;th_ = std::thread(&XThread::Main, this);}virtual void Stop() {is_exit_ = true;Wait();}virtual void Wait() {if (th_.joinable())th_.join();}bool is_exit() {return is_exit_;}private:virtual void Main() = 0;  // 纯虚函数必须要在基类中实现std::thread th_;bool is_exit_ = false;    // 符合谷歌代码风格,私有成员变量后缀加 _
    };
    
  • thread_class.cpp

    #include <iostream>
    #include <thread>
    #include <string>
    #include "XThread.h"using namespace std;/*
    class MyThread {
    public:// 入口线程函数void Main() {cout << "MyThread Main" << name << ": " << age;}string name;int age = 0;
    };
    */class TestXThread : public XThread {
    public:void Main() override {cout << "TestXThread Main begin" << endl;while (!is_exit()) {this_thread::sleep_for(100ms);cout << "." << flush;  // 添加 flush 是为了确保 . 正常输出}cout << "\nTestXThread Main end" << endl;}string name;
    };int main(int argc, char* argv[]) {TestXThread testth;testth.name = "TestXThread name ";testth.Start();this_thread::sleep_for(3s);testth.Stop();testth.Wait();getchar();/*MyThread myth;myth.name = "Test name";myth.age = 20;thread th(&MyThread::Main, &myth);th.join();*/return 0;
    }
    
  • 控制台输出

    TestXThread Main begin
    ............................
    TestXThread Main end
    

7. lambda 临时函数作为线程入口

  • lambda 函数基本格式

    • [捕捉列表] (参数) mutable -> 返回值类型 {函数体}
  • thread_lambda.cpp

    #include <iostream>
    #include <thread>
    #include <string>using namespace std;class TestLambda {
    public:void Start() {thread th([this]() {cout << "name = " << name << endl; });th.join();}string name = "Test Lambda";
    };int main(int argc, char* argv[]) {thread th([](int i) {cout << "test lambda " << i << endl;}, 123);th.join();TestLambda test;test.Start();return 0;
    }
    
  • 控制台输出

    test lambda 123
    name = Test Lambda
    

8. call_once 多线程调用函数只进入一次

初始化函数可以在每一个类型的构造里都调用一遍,不用明确区分,代码可读性提升

  • call_once.cpp

    #include <iostream>
    #include <thread>
    #include <string>
    #include <mutex>using namespace std;void SystemInit() {cout << "Call SystemInit" << endl;
    }void CallOnceSystemInit() {static std::once_flag flag;  // 通过 flag 区分是否只调用一次std::call_once(flag, SystemInit);
    }int main(int argc, char* argv[]) {CallOnceSystemInit();CallOnceSystemInit();for (int i = 0; i < 3; i++) {thread th(CallOnceSystemInit);th.detach();}getchar();return 0;
    }
    
  • 控制台输出

    Call SystemInit
    

相关文章:

C++多线程学习(一):C++11 多线程快速入门

参考引用 C11 14 17 20 多线程从原理到线程池实战代码运行环境&#xff1a;Visual Studio 2019 1. 为什么要用多线程 任务分解 耗时的操作&#xff0c;任务分解&#xff0c;实时响应 数据分解 充分利用多核CPU处理数据 数据流分解 读写分离&#xff0c;解耦合设计 2. 第一个…...

Linux系统之lsof命令的基本使用

Linux系统之lsof命令的基本使用 一、lsof命令的基本使用二、lsof命令的使用帮助2.1 lsof命令的help帮助信息2.2 lsof命令帮助解释 三、lsof的基本使用3.1 直接使用lsof命令3.2 查看某个进程打开的所有文件3.3 查看某个用户打开的所有文件3.4 查看某个文件被哪些进程打开3.5 查看…...

性能压力测试的优势与重要性

性能压力测试是软件开发过程中至关重要的一环&#xff0c;它通过模拟系统在极限条件下的运行&#xff0c;以评估系统在正常和异常负载下的表现。这种测试为确保软件系统的可靠性、稳定性和可伸缩性提供了关键信息。下面将探讨性能压力测试的优势以及为什么在软件开发中它具有不…...

AtCoder Beginner Contest 329 题解A~F

A - Spread 输入字符串&#xff0c;字符之间加上空格输出 B - Next 输出数组当中第二大的数 C - Count xxx 统计每个字符出现过的最长长度&#xff0c;再累加即可 #include<bits/stdc.h> #pragma GCC optimize("Ofast") #define INF 0x3f3f3f3f #define I…...

Windows网络「SSL错误问题」及解决方案

文章目录 问题方案 问题 当我们使用了神秘力量加持网络后&#xff0c;可能会和国内的镜像源网站的之间发生冲突&#xff0c;典型的有 Python 从网络中安装包&#xff0c;如执行 pip install pingouin 时&#xff0c;受网络影响导致无法完成安装的情况&#xff1a; pip config…...

python数据可视化

绘制简单的折线图 1.1json数据格式 JSON是一种轻量级的数据交互格式。可以按照JSON指定的格式去组织和封装数据&#xff0c;其本质上是一个带有特定格式的字符串。 主要功能&#xff1a;json就是一种在各个编程语言中流通的数据格式&#xff0c;负责不同编程语言中的数据传递…...

LV.12 D18 中断处理 学习笔记

一、ARM的异常处理机制及工程代码结构 1.1异常概念 处理器在正常执行程序的过程中可能会遇到一些不正常的事件发生 这时处理器就要将当前的程序暂停下来转而去处理这个异常的事件 异常事件处理完成之后再返回到被异常打断的点继续执行程序。 1.2异常处理机制 不同的处…...

蓝桥杯每日一题2023.11.19

题目描述 “蓝桥杯”练习系统 (lanqiao.cn) 题目分析 首先想到的方法为dfs去寻找每一个数&#xff0c;但发现会有超时 #include<bits/stdc.h> using namespace std; const int N 2e5 10; int n, cnt, a[N]; void dfs(int dep, int sum, int start) {if(dep 4){if(s…...

<b><strong>,<i><em>标签的区别

1. b标签和strong标签 b标签&#xff1a;仅仅是UI层面的加粗样式&#xff0c;并不具备HTML语义 strong标签&#xff1a;不仅是在UI层面的加粗样式&#xff0c;具备HTML语义&#xff0c;表示强调 2. i标签和em标签 i 标签&#xff1a;仅仅是UI层面的斜体样式&#xff0c;并不具备…...

c++中的特殊类设计

文章目录 1.请设计一个类&#xff0c;不能被拷贝2. 请设计一个类&#xff0c;只能在堆上创建对象3. 请设计一个类&#xff0c;只能在栈上创建对象4. 请设计一个类&#xff0c;不能被继承5. 请设计一个类&#xff0c;只能创建一个对象(单例模式) 1.请设计一个类&#xff0c;不能…...

开源更安全? yum源配置/rpm 什么是SSH?

文章目录 1.开放源码有利于系统安全2.yum源配置&#xff0c;这一篇就够了&#xff01;(包括本地&#xff0c;网络&#xff0c;本地共享yum源)3.rpm包是什么4.SSH是什么意思&#xff1f;有什么功能&#xff1f; 1.开放源码有利于系统安全 开放源码有利于系统安全 2.yum源配置…...

庖丁解牛:NIO核心概念与机制详解 04 _ 分散和聚集

文章目录 Pre概述分散/聚集 I/O分散/聚集的应用聚集写入Code Pre 庖丁解牛&#xff1a;NIO核心概念与机制详解 01 庖丁解牛&#xff1a;NIO核心概念与机制详解 02 _ 缓冲区的细节实现 庖丁解牛&#xff1a;NIO核心概念与机制详解 03 _ 缓冲区分配、包装和分片 概述 分散/聚…...

Java读写Jar

Java提供了读写jar的类库Java.util.jar&#xff0c;Java获取解析jar包的工具类如下&#xff1a; import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import java.util.HashMap; import …...

【四元数简述】

w cos(theta/2) x ax * sin(theta/2) y ay * sin(theta/2) z az * sin(theta/2) 向量(x,y,z)是旋转轴 a 是任意正数 theta是旋转角度。 上面就是一个四元数表示旋转。 如何使用 空间中向量&#xff08;1,2,3&#xff09;扩展为&#xff08;0,1,2,3&#…...

ClickHouse SQL 查询优化

1 单表查询 1.1 Prewhere替代where Prewhere和where语句的作用相同&#xff0c;用来过滤数据。不同之处在于prewhere只支持 *MergeTree 族系列引擎的表&#xff0c;首先会读取指定的列数据&#xff0c;来判断数据过滤&#xff0c;等待数据过滤之后再读取select 声明的列字段来补…...

「Verilog学习笔记」数据选择器实现逻辑电路

专栏前言 本专栏的内容主要是记录本人学习Verilog过程中的一些知识点&#xff0c;刷题网站用的是牛客网 分析 将变量A、B接入4选1数据选择器选择输入端S0 S1。将变量C分配在数据输入端。从表中可以看出输出L与变量C的关系。 当AB00时选通D0而此时L0&#xff0c;所以数据端D0接0…...

【Go入门】Web工作方式

【Go入门】 Web工作方式 我们平时浏览网页的时候,会打开浏览器&#xff0c;输入网址后按下回车键&#xff0c;然后就会显示出你想要浏览的内容。在这个看似简单的用户行为背后&#xff0c;到底隐藏了些什么呢&#xff1f; 对于普通的上网过程&#xff0c;系统其实是这样做的&…...

综述:目标检测二十年(机翻版)(未完

原文地址 20年来的目标检测&#xff1a;一项调查 摘要关键词一 介绍二 目标检测二十年A.一个目标检测的路线图1)里程碑&#xff1a;传统探测器Viola Jones探测器HOG检测器基于可变形零件的模型&#xff08;DPM&#xff09; 2)里程碑&#xff1a;基于CNN的两阶段探测器RCNNSPPN…...

quinn源码解析:QUIC数据包是如何发送的

quinn源码解析&#xff1a;QUIC数据包是如何发送的 简介QUIC协议中的概念endpoint&#xff08;端点&#xff09;connection&#xff08;连接&#xff09;Stream&#xff08;流&#xff09;Frame (帧) 发包过程解析SendStream::write_allConnectionDriverEndpointDriver 简介 q…...

scss的高级用法——循环

周末愉快呀&#xff01;一起来学一点简单但非常有用的css小知识。 最近在一个项目中看到以下css class写法&#xff1a; 了解过tailwind css或者unocss的都知道&#xff0c;从命名就可以看出有以下样式&#xff1a; font-size: 30pxmargin-left: 5px;margin-top: 10px; 于是…...

业务系统对接大模型的基础方案:架构设计与关键步骤

业务系统对接大模型&#xff1a;架构设计与关键步骤 在当今数字化转型的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中&#xff0c;不仅可以优化用户体验&#xff0c;还能为业务决策提供…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

React Native 导航系统实战(React Navigation)

导航系统实战&#xff08;React Navigation&#xff09; React Navigation 是 React Native 应用中最常用的导航库之一&#xff0c;它提供了多种导航模式&#xff0c;如堆栈导航&#xff08;Stack Navigator&#xff09;、标签导航&#xff08;Tab Navigator&#xff09;和抽屉…...

2021-03-15 iview一些问题

1.iview 在使用tree组件时&#xff0c;发现没有set类的方法&#xff0c;只有get&#xff0c;那么要改变tree值&#xff0c;只能遍历treeData&#xff0c;递归修改treeData的checked&#xff0c;发现无法更改&#xff0c;原因在于check模式下&#xff0c;子元素的勾选状态跟父节…...

k8s业务程序联调工具-KtConnect

概述 原理 工具作用是建立了一个从本地到集群的单向VPN&#xff0c;根据VPN原理&#xff0c;打通两个内网必然需要借助一个公共中继节点&#xff0c;ktconnect工具巧妙的利用k8s原生的portforward能力&#xff0c;简化了建立连接的过程&#xff0c;apiserver间接起到了中继节…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

听写流程自动化实践,轻量级教育辅助

随着智能教育工具的发展&#xff0c;越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式&#xff0c;也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建&#xff0c;…...

【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制

使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下&#xff0c;限制某个 IP 的访问频率是非常重要的&#xff0c;可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案&#xff0c;使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...

MySQL 索引底层结构揭秘:B-Tree 与 B+Tree 的区别与应用

文章目录 一、背景知识&#xff1a;什么是 B-Tree 和 BTree&#xff1f; B-Tree&#xff08;平衡多路查找树&#xff09; BTree&#xff08;B-Tree 的变种&#xff09; 二、结构对比&#xff1a;一张图看懂 三、为什么 MySQL InnoDB 选择 BTree&#xff1f; 1. 范围查询更快 2…...