Linux-线程池

ops/2024/9/23 6:31:48/

文章目录

  • 前言
  • 一、线程池是什么?
  • 二、示例代码


前言

线程池主要是对之前内容的一个巩固,并且初步了解池化概念。


一、线程池是什么?

线程池就是提前开辟好一块空间,随时准备创造新线程来完成任务,可以理解为用空间来换时间,具体实现看以下示例代码。

二、示例代码

#include <pthread.h>
#include <cstdio>
#include <cstdlib>
#include "lockGuard.hpp"
#include "log.hpp"
const int default_ThreadNum = 5;
template <class T>
class ThreadPool
{public:ThreadPool(int thread_num = default_ThreadNum):_thread_num(thread_num){pthread_mutex_init(&_mutex,nullptr);pthread_cond_init(&_cond,nullptr);for (int i = 1; i <= _thread_num; i++){char nameBuffer[128];snprintf(nameBuffer, sizeof nameBuffer, "Thread %d", i);_threadPool.push_back(new Thread(nameBuffer, routine, (void *)this));logMessage(NORMAL, "%s 线程创建成功!", nameBuffer);}}bool isEmpty(){return _task_queue.empty();}void waitCond(){pthread_cond_wait(&_cond, &_mutex);}pthread_mutex_t &getMutex(){return _mutex;}T getTask(){T task = _task_queue.front();_task_queue.pop();return task;}std::vector<Thread> &getpool(){return _threadPool;}static void *routine(void *args){ThreadData *td = (ThreadData *)args;ThreadPool<T> *tp = (ThreadPool<T> *)td->_args;while (1){T task;{lockGuard lg(&tp->getMutex());while (tp->isEmpty())tp->waitCond();task = tp->getTask();}task(td->_name);}}void run(){for(auto& thread : _threadPool){thread->start();}}void pushTask(const T &task){lockGuard lg(&_mutex);_task_queue.push(task);pthread_cond_signal(&_cond);}~ThreadPool(){for(auto& iter: _threadPool){iter->join();delete iter;}pthread_mutex_destroy(&_mutex);pthread_cond_destroy(&_cond);}private:int _thread_num;std::vector<Thread*> _threadPool;std::queue<T> _task_queue;pthread_mutex_t _mutex;pthread_cond_t _cond;
};


http://www.ppmy.cn/ops/43905.html

相关文章

python纯脚本搬砖DNF之深度学习,工作室适用

声明&#xff1a; 本文章仅作学习交流使用,对产生的任何影响&#xff0c;本人概不负责. 转载请注明出处:https://editor.csdn.net/md?articleId103674748 主要功能 脚本已初步完成&#xff0c;可以上机实战了 1.搬砖研究所、海伯伦&#xff08;持续更新中&#xff09; 2.自…

聚数力 以数兴 | 与“闽”同行,共话数字未来

闽江之畔&#xff0c;数智腾飞。5月24日&#xff0c;第七届数字中国建设峰会在海峡国际会展中心盛大举办。本届展会的主题是“释放数据要素价值&#xff0c;发展新质生产力”&#xff0c;由国家发展改革委、国家数据局、福建省人民政府等单位共同主办&#xff0c;福州市人民政府…

Python文件中动态导入多个.py文件

Python文件中动态导入多个.py文件 一、背景 在一些自动化脚本中,我们需要一些中间文件作为引用文件来处理一些自动化的工作,但是中间文件数量可能根据需求的变更发生不规律的变化,所以就需要一些读文件夹来自动获取这些需要引用的中间文件,下面就是我整理的一个能够实现动…

Hadoop学习之hdfs的操作

Hadoop学习之hdfs的操作 1.将HDFS中的文件复制到本地 package com.shujia.hdfs;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.junit.After; import org.junit.Before; import org.j…

算法刷题笔记 高精度加法(C++实现)

文章目录 题目描述题目思路和代码 题目描述 给定两个正整数&#xff08;不含前导0&#xff09;&#xff0c;计算它们的和。 输入格式 共两行&#xff0c;每行包含一个整数。 输出格式 共一行&#xff0c;包含所求的和。 题目思路和代码 基本思路&#xff1a;模拟竖式计算…

C++复习笔记2

1&#xff0c;构造函数 1.1 实例 1.2 概念 防止忘记初始化&#xff0c;专门给类对象初始化 1.3 备注 2&#xff0c;析造函数 2.1 实例 2.2 概念 防止忘记销毁&#xff0c;专门销毁类对象 2.3 备注 3&#xff0c;默认构造函数 3.1 实例 3.2 概念 编译器自动生成的构造…

深入了解 Golang 多架构编译:交叉编译最佳实践

随着软件开发领域的不断发展&#xff0c;我们面临着越来越多的挑战&#xff0c;其中之一是如何在不同的平台和架构上部署我们的应用程序。Golang&#xff08;Go&#xff09;作为一种现代化的编程语言&#xff0c;具有出色的跨平台支持&#xff0c;通过其强大的多架构编译功能&a…

快速搭建uni-app项目,vue2、Vue3与图鸟UI组件封装

大家好&#xff0c;我们团队近期在uni-app开发领域取得了重要突破&#xff0c;特地向大家介绍一系列基于Vue 2、Vue 3和图鸟UI的封装组件&#xff0c;以及ucharts图表的封装。这些成果旨在帮助开发者们更加高效、便捷地构建uni-app项目。 一、Vue 2、Vue 3与图鸟UI封装组件 为…