线程池学习2
项目地址https://github.com/vit-vit/ctpl辅助队列namespace ctpl { namespace detail { template typename T class Queue { public: bool push(T const value) { std::unique_lockstd::mutex lock(this-mutex); //def名为lock的std::unique_lock智能锁对象,锁住传入的 this-mutex 互斥锁 this-q.push(value); //通过this指针访问当前对象的私有成员q return true; } // deletes the retrieved element, do not use for non integral types // 这里的pop的参数是通过引用带回,输出型参数, 传入一个空杯,在函数里加满水,我再拿回函数 bool pop(T v) { std::unique_lockstd::mutex lock(this-mutex); if (this-q.empty()) return false; v this-q.front(); this-q.pop(); return true; } bool empty() { std::unique_lockstd::mutex lock(this-mutex); return this-q.empty(); } private: std::queueT q; std::mutex mutex; }; }首先封装标准库queue为类模版Queue线程安全的队列实现作为线程池的任务容器。通过this指针访问当前对象的私有成员q所有操作都通过std::unique_lock加锁保证多线程环境下的队列操作安全。thread_pool类之私有成员private: // deleted //这几行代码是 C 中禁用线程池对象的拷贝 / 移动语义的核心写法 // 目的是保证线程池这类 “资源独占型对象” 的安全避免因拷贝 / 移动导致的线程管理混乱、资源泄漏或数据竞争 thread_pool(const thread_pool);// delete; thread_pool(thread_pool);// delete; thread_pool operator(const thread_pool);// delete; thread_pool operator(thread_pool);// delete; void set_thread(int i) { // 让当前创建的工作线程和线程池容器共享同一个 “停止标志”且保证标志的生命周期安全 flag std::shared_ptrstd::atomicbool flag(this-flags[i]); // a copy of the shared ptr to the flag 拷贝构造函数 //Lambda 表达式的类型是 C 编译器匿名的、唯一的闭包类型—— 这个类型没有名字你根本无法手动写出 auto f [this, i, flag/* a copy of the shared ptr to the flag */]() { std::atomicbool _flag *flag; std::functionvoid(int id)* _f; bool isPop this-q.pop(_f); while (true) { while (isPop) { // if there is anything in the queue //用智能指针包裹任务指针自动释放内存 std::unique_ptrstd::functionvoid(int id) func(_f); // at return, delete the function even if an exception occurred //执行任务传入线程索引i (*_f)(i); //检查是否收到停止符号 if (_flag) return; // the thread is wanted to stop, return even if the queue is not empty yet else isPop this-q.pop(_f); //继续取下一个任务 } // the queue is empty here, wait for the next command // 步骤1加锁——保护后续所有临界区操作 std::unique_lockstd::mutex lock(this-mutex); // 步骤2修改等待计数——必须在锁保护下 this-nWaiting; // 步骤3wait 操作——自动解锁休眠 唤醒后重新加锁 (检查是否有新任务/池子销毁/自己线程停止) this-cv.wait(lock, [this, _f, isPop, _flag]() { isPop this-q.pop(_f); return isPop || this-isDone || _flag; }); // 步骤4修改等待计数——依然在锁保护下, 线程被唤醒, 空闲线程的数目减1 --this-nWaiting; // 但如果发现唤醒后不是因为有新任务而是因为要让线程停工或者销毁池子则真的退出 if (!isPop) return; // if the queue is empty and this-isDone true or *flag then return } }; // 释放旧的std::thread, 接替新的std::thread this-threads[i].reset(new std::thread(f)); // compiler may not support std::make_unique() } void init() { this-nWaiting 0; this-isStop false; this-isDone false; } std::vectorstd::unique_ptrstd::thread threads; std::vectorstd::shared_ptrstd::atomicbool flags; detail::Queuestd::functionvoid(int id)* q; //设计为void(int id)是为了把所有任务封装成 接收线程id 无返回值的函数是pool和thread的通用协议 std::atomicbool isDone; std::atomicbool isStop; std::atomicint nWaiting; // how many threads are waiting std::mutex mutex; std::condition_variable cv; };// 工作线程集合智能指针管理自动释放std::vectorstd::unique_ptrstd::thread threads;// 每个线程的停止标志原子类型线程安全std::vectorstd::shared_ptrstd::atomicbool flags;// 任务队列存储函数对象指针detail::Queuestd::functionvoid(int id)* q; // 线程池状态标志原子类型//设计为void(int id)是为把所有任务封装成 接收线程id无返回值的函数是pool和thread的通用协议std::atomicbool isDone; // 是否所有任务都执行完成std::atomicbool isStop; // 是否强制停止线程池std::atomicint nWaiting; // 等待中的线程数量这里的set_thread是创建工作线程的核心函数线程启动后先尝试从队列取任务执行任务执行完后进入等待状态直到有新任务或停止信号等待时释放 CPU 资源避免空转支持随时停止单个线程通过 flag 标志。while(true)里的逻辑有任务_f是从队列取出的任务指针new出来的用unique_ptr包裹后不管是正常执行完还是中途退出func析构时都会自动delete _f彻底避免内存泄漏然后执行任务然后_flag默认是false表示线程未释放所以会循环取下一个任务然后执行。如果任务队列空了isPop会为false也就跳出了while循环然后进入cv段的语句去修改nWaiting解锁然后判断谓语谓语为真唤醒后加锁谓语为假继续休眠。有任务池子未销毁线程未停止会被唤醒。而如果唤醒后发现不是有任务说明唤醒的原因是让线程停工或者池子销毁这个时候则真的退出。再然后部分含义this-threads[i]访问线程池的threads容器vectorunique_ptrthread中第i个元素类型是std::unique_ptrstd::thread.reset(...)调用std::unique_ptr的reset成员方法new std::thread(f)动态创建一个std::thread对象线程执行体是 Lambdaf返回指向该对象的裸指针整体逻辑让threads[i]这个unique_ptr接管新创建的std::thread对象同时自动释放之前管理的线程对象如果有std::thread是 C 标准库的线程类构造时传入 “可调用对象”这里是 Lambdaf

相关新闻

基于Java springboot海洋馆预约系统(源码+文档+运行视频+讲解视频)

基于Java springboot海洋馆预约系统(源码+文档+运行视频+讲解视频)

文章目录 系列文章目录目的前言一、详细视频演示二、项目部分实现截图三、技术栈 后端框架springboot前端框架vue持久层框架MyBaitsPlus系统测试 四、代码参考 源码获取 目的 海洋馆预约系统利用Java Spring Boot框架,实现了游客预约、门票销售、场馆导览及用户反…

2026/5/17 11:53:00 阅读更多 →
# Openssl关键知识

# Openssl关键知识

Openssl关键知识 文章目录Openssl关键知识1 常用命令2 证书扩展段(Extensions Section)3 常用证书类型openssl及证书相关知识一般好像很简单,其实很复杂,这里介绍一些相对关键的部分,以后还会扩展。更多openssl及ca知识…

2026/7/3 10:42:50 阅读更多 →
sdut-程序设计基础Ⅰ-实验一顺序结构(9-15)

sdut-程序设计基础Ⅰ-实验一顺序结构(9-15)

7-9 sdut-C语言-圆柱体计算分数 5作者 马新娟单位 山东理工大学已知圆柱体的底面半径r和高h,计算圆柱体底面周长和面积、圆柱体侧面积以及圆柱体体积。其中圆周率定义为3.1415926。输入格式:输入数据有一行,包括2个正实数r和h,以空格分隔。输…

2026/7/2 19:42:26 阅读更多 →

最新新闻

5分钟上手Flask-profiler:从安装到性能分析的完整教程

5分钟上手Flask-profiler:从安装到性能分析的完整教程

5分钟上手Flask-profiler:从安装到性能分析的完整教程 【免费下载链接】flask-profiler a flask profiler which watches endpoint calls and tries to make some analysis. 项目地址: https://gitcode.com/gh_mirrors/fl/flask-profiler Flask-profiler是一…

2026/7/4 6:30:48 阅读更多 →
Frozen实战案例:如何使用Frozen构建物联网设备配置管理系统

Frozen实战案例:如何使用Frozen构建物联网设备配置管理系统

Frozen实战案例:如何使用Frozen构建物联网设备配置管理系统 【免费下载链接】frozen JSON parser and generator for C/C with scanf/printf like interface. Targeting embedded systems. 项目地址: https://gitcode.com/gh_mirrors/fro/frozen 在物联网设备…

2026/7/4 6:30:47 阅读更多 →
Windmill React UI黑暗模式实战:轻松实现优雅的深色主题切换

Windmill React UI黑暗模式实战:轻松实现优雅的深色主题切换

Windmill React UI黑暗模式实战:轻松实现优雅的深色主题切换 【免费下载链接】windmill-react-ui 🧩 The component library for fast and accessible development of gorgeous interfaces. 项目地址: https://gitcode.com/gh_mirrors/wi/windmill-rea…

2026/7/4 6:30:47 阅读更多 →
translate-python高级技巧:自定义翻译 provider 与错误处理最佳实践

translate-python高级技巧:自定义翻译 provider 与错误处理最佳实践

translate-python高级技巧:自定义翻译 provider 与错误处理最佳实践 【免费下载链接】translate-python Online translation as a Python module & command line tool. No key, no authentication needed. 项目地址: https://gitcode.com/gh_mirrors/tr/trans…

2026/7/4 6:28:47 阅读更多 →
FPDF版本1.9新特性解析:最新功能与改进

FPDF版本1.9新特性解析:最新功能与改进

FPDF版本1.9新特性解析:最新功能与改进 【免费下载链接】FPDF FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. 项目地址: https…

2026/7/4 6:28:47 阅读更多 →
nginx-auth-ldap性能优化终极指南:连接池配置与缓存策略提升认证效率

nginx-auth-ldap性能优化终极指南:连接池配置与缓存策略提升认证效率

nginx-auth-ldap性能优化终极指南:连接池配置与缓存策略提升认证效率 【免费下载链接】nginx-auth-ldap LDAP authentication module for nginx 项目地址: https://gitcode.com/gh_mirrors/ng/nginx-auth-ldap nginx-auth-ldap是一个强大的LDAP认证模块&…

2026/7/4 6:26:47 阅读更多 →

日新闻

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 正式发布,这是一个关键的安全修复版本,修复了多个方面的问题,还对部分功能进行了优化。 安全修复亮点 此次发布在安全修复上表现突出。binprot 避免了项目引用计数溢出,mcmc 因安全问题提升了上游版本号&#xf…

2026/7/4 0:04:29 阅读更多 →
终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案 【免费下载链接】HMCL A Minecraft Launcher which is multi-functional, cross-platform and popular 项目地址: https://gitcode.com/gh_mirrors/hm/HMCL HMCL(Hello Minecraft! Lau…

2026/7/4 0:06:29 阅读更多 →
KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

1. KMX63与PIC18F66K40的硬件协同架构解析KMX63作为一款三轴加速度计和磁力计组合传感器,与PIC18F66K40微控制器的搭配堪称嵌入式HMI开发的黄金组合。这套硬件组合的核心优势在于KMX63提供的高精度运动感知能力与PIC18F66K40强大的信号处理能力形成了完美互补。KMX6…

2026/7/4 0:06:29 阅读更多 →

周新闻

月新闻