当前位置: 首页 > 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; 于是…...

国防科技大学计算机基础课程笔记02信息编码

1.机内码和国标码 国标码就是我们非常熟悉的这个GB2312,但是因为都是16进制&#xff0c;因此这个了16进制的数据既可以翻译成为这个机器码&#xff0c;也可以翻译成为这个国标码&#xff0c;所以这个时候很容易会出现这个歧义的情况&#xff1b; 因此&#xff0c;我们的这个国…...

大数据学习栈记——Neo4j的安装与使用

本文介绍图数据库Neofj的安装与使用&#xff0c;操作系统&#xff1a;Ubuntu24.04&#xff0c;Neofj版本&#xff1a;2025.04.0。 Apt安装 Neofj可以进行官网安装&#xff1a;Neo4j Deployment Center - Graph Database & Analytics 我这里安装是添加软件源的方法 最新版…...

Java 8 Stream API 入门到实践详解

一、告别 for 循环&#xff01; 传统痛点&#xff1a; Java 8 之前&#xff0c;集合操作离不开冗长的 for 循环和匿名类。例如&#xff0c;过滤列表中的偶数&#xff1a; List<Integer> list Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens new ArrayList…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

渲染学进阶内容——模型

最近在写模组的时候发现渲染器里面离不开模型的定义,在渲染的第二篇文章中简单的讲解了一下关于模型部分的内容,其实不管是方块还是方块实体,都离不开模型的内容 🧱 一、CubeListBuilder 功能解析 CubeListBuilder 是 Minecraft Java 版模型系统的核心构建器,用于动态创…...

使用van-uploader 的UI组件,结合vue2如何实现图片上传组件的封装

以下是基于 vant-ui&#xff08;适配 Vue2 版本 &#xff09;实现截图中照片上传预览、删除功能&#xff0c;并封装成可复用组件的完整代码&#xff0c;包含样式和逻辑实现&#xff0c;可直接在 Vue2 项目中使用&#xff1a; 1. 封装的图片上传组件 ImageUploader.vue <te…...

【算法训练营Day07】字符串part1

文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接&#xff1a;344. 反转字符串 双指针法&#xff0c;两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

Python爬虫(一):爬虫伪装

一、网站防爬机制概述 在当今互联网环境中&#xff0c;具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类&#xff1a; 身份验证机制&#xff1a;直接将未经授权的爬虫阻挡在外反爬技术体系&#xff1a;通过各种技术手段增加爬虫获取数据的难度…...

关于 WASM:1. WASM 基础原理

一、WASM 简介 1.1 WebAssembly 是什么&#xff1f; WebAssembly&#xff08;WASM&#xff09; 是一种能在现代浏览器中高效运行的二进制指令格式&#xff0c;它不是传统的编程语言&#xff0c;而是一种 低级字节码格式&#xff0c;可由高级语言&#xff08;如 C、C、Rust&am…...

【JavaWeb】Docker项目部署

引言 之前学习了Linux操作系统的常见命令&#xff0c;在Linux上安装软件&#xff0c;以及如何在Linux上部署一个单体项目&#xff0c;大多数同学都会有相同的感受&#xff0c;那就是麻烦。 核心体现在三点&#xff1a; 命令太多了&#xff0c;记不住 软件安装包名字复杂&…...