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
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/148665.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

本地Git项目同时推送至GitHub和Gitee

分别在gitee和github新建一个仓库 github: gitee: 添加远程仓库 git remote add origin1 [你的GitHub仓库URL] git remote add origin2 [你的Gitee仓库URL] 在本地中初始化创建一个git本地分支 git init 进入.git目录下修改config文件 [remote "origin"] url g…

VUE(一)

1.vue简介 英文官网: Vue.js - The Progressive JavaScript Framework | Vue.js 中文官网: Vue.js - 渐进式 JavaScript 框架 | Vue.js 2.Vue的特点 3.初识VUE 在官网下载VUE.js,有两个版本&#xff0c;一个开发一个生产 <!DOCTYPE html> <html lang"en"…

重磅 | 进一步夯实生态建设,朗思科技与阿里龙蜥完成兼容性认证

近日&#xff0c;北京朗思智能科技有限公司&#xff08;以下简称“朗思科技”&#xff09;自主研发的数字员工产品与OpenAnolis龙蜥社区龙蜥操作系统&#xff08;Anolis OS&#xff09;8完成兼容性认证。测试结果显示&#xff0c;双方产品相互兼容&#xff0c;功能正常&#xf…

电子学会C/C++编程等级考试2022年03月(一级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:双精度浮点数的输入输出 输入一个双精度浮点数,保留8位小数,输出这个浮点数。 时间限制:1000 内存限制:65536输入 只有一行,一个双精度浮点数。输出 一行,保留8位小数的浮点数。样例输入 3.1415926535798932样例输出 3.1…

Codeforces Round 910 (Div. 2)(D~F)

1898D - Absolute Beauty 题意&#xff1a;给定长度为n的数组a和b&#xff0c;定义b数组的价值为&#xff0c;现可以交换一次b数组中的任意两个元素&#xff0c;求b数组的价值最大值。 思路&#xff1a;绝对值问题可以放在数轴上去解决。绝对值即为区间长度 观察上述三种情况&…

计算机毕业设计选题推荐-点餐微信小程序/安卓APP-项目实战

✨作者主页&#xff1a;IT毕设梦工厂✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Py…

【c++随笔13】多态

【c随笔13】多态 多态性&#xff08;Polymorphism&#xff09;在面向对象编程中是一个重要概念&#xff0c;它允许以统一的方式处理不同类型的对象&#xff0c;并在运行时动态确定实际执行的方法或函数。一、什么是多态性&#xff1f;1、关键概念&#xff1a;C的多态性2、多态定…

docker容器内访问主机端口服务

docker run 加参数: --add-host"host.docker.internal:host-gateway" 然后就可以: 在docker容器内 curl host.docker.internal:xxxx 通了 然而 其他服务(数据库等)可以访问通了&#xff0c;但是redis服务连接被拒绝,发现是 redis要绑 docker网卡 把docke网卡ip …

14 Go的类型转换

概述 在上一节的内容中&#xff0c;我们介绍了Go的错误处理&#xff0c;包括&#xff1a;errors包、返回错误、抛出异常、捕获异常等。在本节中&#xff0c;我们将介绍Go的类型转换。在Go语言中&#xff0c;类型转换是一种将一个值从一种类型转换为另一种类型的过程。类型转换主…

如何提升自己?

来自鱼皮大佬&#xff1a;链接 1、每日阅读 每日读2-3篇文章&#xff0c;包括行业趋势、技术干货、经验分享、思维提升 大厂的技术博客&#xff1a;比如美团技术团队、阿里技术团队科技资讯类&#xff1a;量子位、差评、新智元、无敌信息差经验分享、编程趋势、技术干货&…

SpringCloud微服务注册中心:Nacos介绍,微服务注册,Ribbon通信,Ribbon负载均衡,Nacos配置管理详细介绍

微服务注册中心 注册中心可以说是微服务架构中的”通讯录“&#xff0c;它记录了服务和服务地址的映射关系。在分布式架构中&#xff0c;服务会注册到这里&#xff0c;当服务需要调用其它服务时&#xff0c;就这里找到服务的地址&#xff0c;进行调用。 微服务注册中心 服务注…

OpenGL_Learn13(材质)

1. 材质 cube.vs #version 330 core layout (location 0) in vec3 aPos; layout (location 0 ) in vec3 aNormal;out vec3 FragPos; out vec3 Normal;uniform mat4 model; uniform mat4 view; uniform mat4 projection;void main() {FragPosvec3(model*vec4(aPos,1.0));Norma…

17. Spring类型转换之PropertyEditor

简介 我们之所以使用spring如此的简单&#xff0c;并且功能比较强大&#xff0c;比较人性化&#xff0c;是因为很多重复且繁琐的以及能想到的可以简化业务的功能都给我们默认提供了&#xff0c;类型转化就是典型的例子 spring提供了两种类型转化的方式&#xff0c;一种是spri…

从零开始:Rust环境搭建指南

大家好&#xff01;我是lincyang。 今天&#xff0c;我们将一起探讨如何从零开始搭建Rust开发环境。 Rust环境搭建概览 Rust是一种系统编程语言&#xff0c;以其安全性、并发性和性能闻名。搭建Rust环境是学习和使用这一语言的第一步。 第一步&#xff1a;安装Rust Rust的…

3-docker安装centos7

CentOS7.9下安装完成docker后&#xff0c;后续我们可以在其上安装centos7系统。具体操作如下&#xff1a; 1.以root用户登录CentOS7.9服务器&#xff0c;拉取centos7 images 命令&#xff1a; docker pull centos:centos7 2.加载centos7 images并登录验证 命令&#xff1a;…

Activiti7工作流

文章目录 一、工作流介绍1.1 概念1.2 适用行业1.3 应用领域1.4 传统实现方式1.5 什么是工作流引擎 二、什么是Activiti7&#xff1f;2.1 概述2.2 Activiti7内部核心机制2.3 BPMN2.4 Activiti如何使用2.4.1 整合Activiti2.4.2 业务流程建模2.4.3 部署业务流程2.4.4 启动流程实例…

python语法之变量名

变量可以有一个简短的名称(如x和y)&#xff0c;也可以有一个更具描述性的名称(如age、carname、total_volume)。Python变量规则: 变量名必须以字母或下划线开头变量名不能以数字开头变量名只能包含字母数字字符和下划线(A-z、0-9和_)。变量名区分大小写(age、age和age是三个不…

WPF中行为与触发器的概念及用法

完全来源于十月的寒流&#xff0c;感谢大佬讲解 一、行为 (Behaviors) behaviors的简单测试 <Window x:Class"Test_05.MainWindow"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"http://schemas.microsoft.com/winf…

DevToys:开发者的多功能瑞士军刀,让编程更高效!

DevToys&#xff1a;开发者的多功能瑞士军刀&#xff0c;让编程更高效&#xff01; DevToys 是一款专为开发者设计的实用工具&#xff0c;它能够帮助用户完成日常的开发任务&#xff0c;如格式化 JSON、比较文本和测试正则表达式&#xff08;RegExp&#xff09;。它的优势在于…

Selenium UI 自动化

一、Selenium 自动化 1、什么是Selenium&#xff1f; Selenium是web应用中基于UI的自动化测试框架。 2、Selenium的特点&#xff1f; 支持多平台、多浏览器、多语言。 3、自动化工作原理&#xff1f; 通过上图&#xff0c;我们可以注意到3个角色&#xff0c;下面具体讲解一…