C++ Thread多线程并发记录(3)线程创建总结

news/2024/10/21 7:35:38/

1.启动线程传递全局函数

#include <iostream>
#include <thread>void Th1(int id){std::cout << "Create global fun. id = " << id << std::endl;
}
void TH1(const int &id){std::cout << "Create global fun. id = " << id << std::endl;
}int main() {std::thread th1(Th1, 1);const int &num = 2;std::thread th2(Th1,std::ref(num));th1.join();//等待子线程结束后汇聚,注意同一个thread对象join两次会产生未定义行为th2.join();//th2.detach();//detach方法将线程变为守护进程,有可能出现主线程结束后,子线程还未结束的情况,故而不要使用detach线程访问栈内存return 0;
}

2.启动线程传递类成员函数

#include <iostream>
#include <string>
#include <thread>class TH1{
public:void print(){std::cout << name << std::endl;}std::string name{};
};
class TH2{
public:void operator()(std::string name) {std::cout << name << std::endl;}
};int main() {TH1 t1;t1.name = "this is thread 1.";std::thread th1(&TH1::print, &t1);//传递类成员函数地址以及类实例地址std::thread th2((TH2()), "this is thread 2.");th1.join();th2.join();return 0;
}

3.传递Lambda表达式

#include <iostream>
#include <string>
#include <thread>int main() {std::string name{"This is thread 1."};std::thread t1([](const std::string &s){ std::cout << s << std::endl; }, std::ref(name));t1.join();return 0;
}

4.Thread RAII管理线程

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <thread>class Thread_join {
public:Thread_join() = default;virtual ~Thread_join() = default;virtual void start() {thread_ = std::thread(&Thread_join::do_something, this); //传入基类指针产生动态绑定,根据动态类型选择虚函数类型};virtual void wait(){if (thread_.joinable())thread_.join();}
protected:std::thread thread_; //使用智能指针管理thread需要自定义virtual void do_something() = 0; //类中方法声明为纯虚函数
};class TH1 : public Thread_join {
public:void do_something() override {for (const auto &i : s)std::cout << i << " ";}
private:std::vector<std::string> s{"This", "is", "thread", "RAII"};
};int main() {TH1 t;t.start();t.wait();return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <thread>class Thread_join {
public:Thread_join() = default;virtual ~Thread_join(){if (thread_.joinable())thread_.join();}virtual void start() {thread_ = std::thread(&Thread_join::do_something, this); //传入基类指针产生动态绑定,根据动态类型选择虚函数类型};virtual void wait(){if (thread_.joinable())thread_.join();}
protected:std::thread thread_; //使用智能指针管理thread需要自定义virtual void do_something() = 0; //类中方法声明为纯虚函数
};class TH1 : public Thread_join {
public:~TH1() override{if (thread_.joinable())thread_.join();}void do_something() override {for (const auto &i : s)std::cout << i << " ";}
private:std::vector<std::string> s{"This", "is", "thread", "RAII"};
};int main() {TH1 t;t.start();return 0;
}


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

相关文章

2024.5.31学习记录

1、面经复习&#xff1a;webpack 2、rosebush 开发完成&#xff1a; rosebush官网

拍视频麦克风什么牌子好?户外无线麦克风哪个牌子好,看本期文章

​无线领夹麦克风&#xff0c;作为现代音频技术的重要代表&#xff0c;已经广泛应用于各种场合。它不仅能提高演讲者的声音质量&#xff0c;还能增加演讲的互动性和生动性。然而&#xff0c;面对市场上众多的无线领夹麦克风产品&#xff0c;如何选择一款适合自己的设备成为了一…

PAT甲级真题刷题笔记

持续更新&#xff0c;记得点赞收藏关注&#xff01; 目录 英文专题题目按分类简单数字数学问题模拟排序、查找、二分贪心动态规划栈、队列、链表堆树、图、并查集 英文专题 polynomials 多项式 即 形如a x^b c x^d exponents and coefficients 指数和系数 tie 平局 radix基数…

Socket网络通讯入门(一)

提示&#xff1a;能力有限&#xff0c;不足以及错误之处还请指出&#xff01; 文章目录 前言一、 计算机网络 OSI、TCP/IP、五层协议 体系结构1.OSI七层模型每层的作用2.TCP/IP协议分成3.五层协议体系结构 二、Socket服务端和客户端 简单通信1.服务端代码2.客户端 总结 前言 简…

摄像头对人脸进行性别和年龄的判断

摄像头对人脸性别和年龄判断 导入必要的库加载预训练的人脸检测模型加载预训练的性别和年龄识别模型定义性别和年龄的标签列表打开摄像头从摄像头读取一帧转换为灰度图像检测人脸遍历检测到的人脸显示视频流按 ‘q’ 或点击窗口的“”退出循环释放摄像头和销毁所有窗口全部代码…

【TB作品】msp430f5529单片机,读取DHT11温湿度,读取adc,oled显示

功能 msp430f5529单片机&#xff0c;读取DHT11温湿度&#xff0c;读取adc&#xff0c;oled显示 硬件 //OLED引脚分配 绿色板子 //DO(SCLK)------P4.3 //D1(DATA)------P4.0 //RES-----------P3.7 //DC------------P8.2 //CS------------P8.1 //mq135 P6.5 //DHT11 P4.1 部…

盲盒系统开发功能架构 源码搭建 定制开发

一、盲盒为何热度这么高&#xff1f; 盲盒热的形成&#xff0c;是产品、消费者和市场三方合力的结果&#xff1a; 从产品看&#xff1a;盲盒产品线比较丰富&#xff0c;新品层出不穷&#xff0c;加上隐藏款或特别款的“双重诱惑”&#xff0c;更加激发盲盒收藏爱好者的收藏欲…

用于脑肿瘤分割的跨模态深度特征学习| 文献速递-深度学习肿瘤自动分割

Title 题目 Cross-modality deep feature learning for brain tumor segmentation 用于脑肿瘤分割的跨模态深度特征学习 01 文献速递介绍 作为最致命的流行病&#xff0c;脑肿瘤的研究越来越受到关注。本文研究了一种基于深度学习的自动分割胶质瘤的方法&#xff0c;称为脑…