当前位置: 首页 > news >正文

创建线程池和封装锁

封装一个锁

1.封装一个Mutex

class Mutex{public:Mutex(pthread_mutex_t * lock):_lock(lock){}void Lock(){pthread_mutex_lock(_lock);}void unLock(){pthread_mutex_unlock(_lock);}~Mutex(){}private:pthread_mutex_t *_lock;
};

2.封装一个LockGuard

class LockGuard{public:LockGuard(pthread_mutex_t * lock):_mutex(lock){_mutex.Lock();}~LockGuard(){_mutex.unLock();}private:Mutex _mutex;
};

在底层就可以直接创建一个LockGuard对象,就可以直接对临时资源进行加锁

类似:

​
{LockGuard(&mutex);//临界资源}​

创建线程池

封装线程

1.成员变量

private:std::string _threadname;pthread_t _pid;func_t<T> _func;bool _isrunning;T _data;
template <class T>
using func_t = std::function<void(T&)>;

我们创建一个fun_t 用来传递一个函数给线程,在内部用来回调。

2.构造函数

  Thread(std::string threadname, func_t<T> func, T& data): _threadname(threadname), _pid(0), _func(func), _isrunning(false), _data(data){}

3.Start函数和Join函数

 bool Start(){int n = pthread_create(&_pid, nullptr, ThreadRoutine, this);if (n == 0){_isrunning = true;return true;}return false;}bool Join(){if(!_isrunning) return true;int n=pthread_join(_pid,nullptr);if(n==0){_isrunning=false;return true;}else{return false;}}

4.在创建线程传递一个ThreadRoution函数

static void *ThreadRoutine(void *args) // 类内方法,{// (void)args; // 仅仅是为了防止编译器有告警Thread *ts = static_cast<Thread *>(args);ts->_func(ts->_data);return nullptr;}

ThreadRountion可不可以为类内函数?

不可以,类内函数默认隐藏的传递this指针,我们必须设置为静态函数函数,再传递this指针。

#pragma once#include <iostream>
#include <pthread.h>
#include <functional>
#include <cstring>template <class T>
using func_t = std::function<void(T&)>;template <class T>
class Thread
{
public:Thread(std::string threadname, func_t<T> func, T& data): _threadname(threadname), _pid(0), _func(func), _isrunning(false), _data(data){}static void *ThreadRoutine(void *args) // 类内方法,{// (void)args; // 仅仅是为了防止编译器有告警Thread *ts = static_cast<Thread *>(args);ts->_func(ts->_data);return nullptr;}bool Start(){int n = pthread_create(&_pid, nullptr, ThreadRoutine, this);if (n == 0){_isrunning = true;return true;}return false;}bool Join(){if(!_isrunning) return true;int n=pthread_join(_pid,nullptr);if(n==0){_isrunning=false;return true;}else{return false;}}const std::string& Threadname(){return _threadname;} private:std::string _threadname;pthread_t _pid;func_t<T> _func;bool _isrunning;T _data;
};

创建线程池

我们打算用queue来储存任务,用vector数组储存线程

当任务队列为空时,我们用条件变量去控制线程睡眠,队列Push进任务再控制线程醒来。

1.成员变量

private:queue<T> _taskq;vector<Thread<ThreadData>> _ptdv;pthread_mutex_t _mutex;pthread_cond_t _cond;int _thread_num;

2.构造函数

初始化锁,条件变量,并创建线程。

  pthread_pool(int thread_num = defaultthreadnum) : _thread_num(thread_num){pthread_mutex_init(&_mutex, nullptr);pthread_cond_init(&_cond, nullptr);for (int i = 1; i <= defaultthreadnum; i++){string threadname("thread-");threadname += to_string(i);ThreadData td(threadname);Thread<ThreadData> t(threadname,std::bind(&pthread_pool<T>::pthread_Run, this, std::placeholders::_1), td);_ptdv.push_back(t);lg.LogMessage(Info, "%s is created...\n", threadname.c_str());}}

任务队列为空,让所有线程休眠,队列中有任务,直接让线程执行任务:t()

void pthread_Run(ThreadData &data){while (true){T t;{LockGuard ld(&_mutex);if (_taskq.empty()){allthreadsleep(data);}t = _taskq.front();_taskq.pop();}t();{LockGuard ld(&_mutex);lg.LogMessage(Info, "Thread name:%s is handling: %s \n", data._name.c_str(), t.PrintTask().c_str());lg.LogMessage(Info, "Thread name:%s get result: %s \n", data._name.c_str(), t.PrintResult().c_str());}}}

3.进程唤醒与进程休眠

void allthreadwakeup(){pthread_cond_signal(&_cond);}
void allthreadsleep(const ThreadData &data){lg.LogMessage(Debug, "no task, %s is sleeping...\n", data._name.c_str());pthread_cond_wait(&_cond, &_mutex);}

4.Push()

    void Push(T &task){{LockGuard ld(&_mutex);_taskq.push(task);// 插入任务后唤醒进程allthreadwakeup();}}
#pragma once
#include <iostream>
#include <vector>
#include <queue>
#include <pthread.h>
#include <cstring>
#include <functional>
#include <unistd.h>
#include "pthread.hpp"
#include "LockGuard.hpp"
#include "Log.hpp"
using namespace std;
static const int defaultthreadnum = 5;template <class T>
using func_r = std::function<void(T)>;class ThreadData
{
public:ThreadData(string name) : _name(name){}~ThreadData(){}public:string _name;
};
template <class T>
class pthread_pool
{
public:static pthread_pool<T> *Getinstance(){if (instance == nullptr){pthread_mutex_lock(&_sig_ins);if (instance == nullptr){instance = new pthread_pool<T>(defaultthreadnum);lg.LogMessage(Info, "instance is create succeess...\n");return instance;}}}pthread_pool(int thread_num = defaultthreadnum) : _thread_num(thread_num){pthread_mutex_init(&_mutex, nullptr);pthread_cond_init(&_cond, nullptr);for (int i = 1; i <= defaultthreadnum; i++){string threadname("thread-");threadname += to_string(i);ThreadData td(threadname);Thread<ThreadData> t(threadname,std::bind(&pthread_pool<T>::pthread_Run, this, std::placeholders::_1), td);_ptdv.push_back(t);lg.LogMessage(Info, "%s is created...\n", threadname.c_str());}}pthread_pool(const pthread_pool<T> &pp) = delete;const pthread_pool<T> &operator=(const pthread_pool<T> &pp) = delete;void allthreadwakeup(){pthread_cond_signal(&_cond);}void allthreadsleep(const ThreadData &data){lg.LogMessage(Debug, "no task, %s is sleeping...\n", data._name.c_str());pthread_cond_wait(&_cond, &_mutex);}void pthread_Run(ThreadData &data){while (true){T t;{LockGuard ld(&_mutex);if (_taskq.empty()){allthreadsleep(data);}t = _taskq.front();_taskq.pop();}t();{LockGuard ld(&_mutex);lg.LogMessage(Info, "Thread name:%s is handling: %s \n", data._name.c_str(), t.PrintTask().c_str());lg.LogMessage(Info, "Thread name:%s get result: %s \n", data._name.c_str(), t.PrintResult().c_str());}}}void Start(){for (auto &th : _ptdv){th.Start();lg.LogMessage(Info, "%s is running ...\n", th.Threadname().c_str());}}void Push(T &task){{LockGuard ld(&_mutex);_taskq.push(task);// 插入任务后唤醒进程allthreadwakeup();}}// Just for debug success!void Wait(){for (auto &th : _ptdv){th.Join();}}~pthread_pool(){pthread_mutex_destroy(&_mutex);pthread_cond_destroy(&_cond);}private:queue<T> _taskq;vector<Thread<ThreadData>> _ptdv;pthread_mutex_t _mutex;pthread_cond_t _cond;int _thread_num;static pthread_pool *instance;static pthread_mutex_t _sig_ins;
};
template <class T>
pthread_pool<T> *pthread_pool<T>::instance = nullptr;
template <class T>
pthread_mutex_t pthread_pool<T>::_sig_ins = PTHREAD_MUTEX_INITIALIZER;

线程池单利化问题

  static pthread_pool<T> *Getinstance(){if (instance == nullptr){pthread_mutex_lock(&_sig_ins);if (instance == nullptr){instance = new pthread_pool<T>(defaultthreadnum);lg.LogMessage(Info, "instance is create succeess...\n");return instance;}}}

在获取单例的函数中,我们是需要加锁的,防止多个线程同时进入创建出多个单利。

相关文章:

创建线程池和封装锁

封装一个锁 1.封装一个Mutex class Mutex{public:Mutex(pthread_mutex_t * lock):_lock(lock){}void Lock(){pthread_mutex_lock(_lock);}void unLock(){pthread_mutex_unlock(_lock);}~Mutex(){}private:pthread_mutex_t *_lock; };2.封装一个LockGuard class LockGuard{pub…...

易图讯军用VR三维电子沙盘系统

深圳易图讯军用VR三维电子沙盘系统是一种集成了虚拟现实&#xff08;VR&#xff09;技术、三维建模技术、大数据分析、实时动态更新以及高度安全可靠的综合性军事指挥平台。该系统通过高精度三维模型真实再现战场环境&#xff0c;为指挥员提供沉浸式体验和交互操作的可能性&…...

LeetCode讲解篇之70. 爬楼梯

文章目录 题目描述题解思路题解代码题目链接 题目描述 题解思路 爬楼梯有一个规律&#xff0c;爬到第n层楼梯的方法种数 爬到第n - 1层楼梯的方法种数 爬到第n - 1层楼梯的方法种数 也就是我们爬到第n层楼梯其实是从第n - 1层楼梯向上爬1层或者是n - 2层楼梯向上爬2层转换来…...

论文写作不再难,论文初稿快速成型法!

撰写论文是每个学者的必修课&#xff0c;我非常明白撰写论文的不易。撰写过程中会遇到各种困扰&#xff0c;如思路不清晰、论证不充分、语言表达不准确等。在这里以我的经验分享给大家一个能快速完成论文初稿的秘诀“AI导师写作”&#xff0c;希望能帮助还在为论文发愁的你。 …...

linux系统,监控进程运行状态并自动重启崩溃后的进程的多种方法

系统进程运行异常崩溃后&#xff0c;自动重启的方法 有的公司&#xff0c;会写monitor守护进程&#xff0c;监视各个进程的运行状态&#xff0c;异常时&#xff0c;自动重启&#xff0c;但是这种&#xff0c;通过一个进程 监护一个进程的做法&#xff0c;不太完美&#xff0c;…...

【JavaEE初阶】深入理解不同锁的意义,synchronized的加锁过程理解以及CAS的原子性实现(面试经典题);

前言 &#x1f31f;&#x1f31f;本期讲解关于锁的相关知识了解&#xff0c;这里涉及到高频面试题哦~~~ &#x1f308;上期博客在这里&#xff1a;【JavaEE初阶】深入理解线程池的概念以及Java标准库提供的方法参数分析-CSDN博客 &#x1f308;感兴趣的小伙伴看一看小编主页&am…...

详解Redis分布式锁在SpringBoot的@Async方法中没锁住的坑

背景 Redis分布式锁很有用处&#xff0c;在秒杀、抢购、订单、限流特别是一些用到异步分布式并行处理任务时频繁的用到&#xff0c;可以说它是一个BS架构的应用中最高频使用的技术之一。 但是我们经常会碰到这样的一个问题&#xff0c;那就是我们都按照标准做了但有时运行着、…...

怎么做接口自动化测试

在分层测试的“金字塔”模型中&#xff0c;接口测试属于第二层服务集成测试范畴。相比UI层&#xff08;主要是WEB或APP&#xff09;自动化测试而言&#xff0c;接口自动化测试收益更大&#xff0c;且容易实现&#xff0c;维护成本低&#xff0c;有着更高的投入产出比&#xff0…...

网络编程(18)——使用asio协程实现并发服务器

十八、day18 到目前为止&#xff0c;我们以及学习了单线程同步/异步服务器、多线程IOServicePool和多线程IOThreadPool模型&#xff0c;今天学习如何通过asio协程实现并发服务器。 并发服务器有以下几种好处&#xff1a; 协程比线程更轻量&#xff0c;创建和销毁协程的开销较…...

Koa2项目实战2(路由管理、项目结构优化)

添加路由&#xff08;处理不同的URL请求&#xff09; 路由&#xff1a;根据不同的URL&#xff0c;调用对应的处理函数。 每一个接口服务&#xff0c;最核心的功能是&#xff1a;根据不同的URL请求&#xff0c;返回不同的数据。也就是调用不同的接口返回不同的数据。 在 Node…...

决战Linux操作系统

前言&#xff1a; 你是否也曾经为Linux所困扰过&#xff0c;在网上找的资料零零散散&#xff0c;是否学完Linux后还是懵懵懂懂&#xff0c;别怕&#xff0c;这篇博客是博主精心为你准备的&#xff0c;现在&#xff0c;就让我们一起来走进Linux的世界&#xff0c;决战Linux&…...

OceanBase 3.2.2 数据库问题处理记录

只记录OceanBase 数据库与OCP的异常处理&#xff0c;其它组件暂时不写录。 一、问题1&#xff1a; 说明&#xff1a;OMS 出现异常&#xff0c;无法访问(OB无法访问) OB数据库架构&#xff1a;1:1:1 原因&#xff1a;某一台OBserver因为内存问题&#xff0c;被服务器直接kill掉…...

HCIP--以太网交换安全(二)端口安全

端口安全 一、端口安全概述 1.1、端口安全概述&#xff1a;端口安全是一种网络设备防护措施&#xff0c;通过将接口学习的MAC地址设为安全地址防止非法用户通信。 1.2、端口安全原理&#xff1a; 类型 定义 特点 安全动态MAC地址 使能端口而未是能Stichy MAC功能是转换的…...

在 Windows 11 安卓子系统中安装 APK 的操作指南

这个软件好像不可以在纯android系统中使用&#xff08;不知道是缺了什么&#xff09;&#xff0c;其他对于android的虚拟机要不缺少必要功能组件&#xff0c;要不性能过于低下。本方法致力于在带有谷歌框架WSA中运行该APK 在 Windows 11 安卓子系统中安装 APK 的操作指南 本指…...

[C语言] 函数详解:库函数与自定义函数

文章目录 函数的概念库函数和自定义函数库函数使用库函数示例常用库函数及头文件 自定义函数自定义函数的基本结构示例&#xff1a;实现两个数的求和函数自定义函数的好处 函数的返回值有返回值的函数无返回值的函数 函数的声明与调用声明函数在另一个文件中调用函数示例&#…...

0x11 科迈 RAS系统 Cookie验证越权漏洞

参考: 科迈 RAS系统 Cookie验证越权漏洞 | PeiQi文库 (wgpsec.org)免责声明 欢迎访问我的博客。以下内容仅供教育和信息用途: 合法性:我不支持或鼓励非法活动。请确保遵守法律法规。信息准确性:尽管我尽力提供准确的信息,但不保证其完全准确或适用。使用前请自行验证。风…...

MoonBit 双周报 Vol.57:AI助手功能增强、表达式优先级调整、JS 交互优化、标准库与实验库API多项更新!

2024-10-08 IDE更新 AI Codelens支持 /generate 和 /fix 命令 /generate 命令能够提供一个通用的用以生成代码的聊天界面。 /fix 命令能够读取当前函数的错误信息给出修复建议。 MoonBit更新 调整中缀表达式和if、match、loop、while、for、try表达式的优先级, 后者这些控制…...

element ui input textarea控制显示高度

样式代码 .testPage { position: absolute; left: 0; top: 0; right: 0; bottom: 0; display: flex; height: 100%; /* 控制输入框高度 */ .el-textarea { height: 90%; ::v-deep .el-textarea__inner { height: 90%; } } }...

Chromium 中chrome.downloads扩展接口c++

一、前端chrome.downloads 使用 chrome.downloads API 以编程方式启动、监控、操作和搜索下载内容。 权限 downloads 您必须在扩展程序清单中声明 "downloads" 权限&#xff0c;才能使用此 API。 {"name": "My extension",..."permiss…...

微信小程序常见问题

一、编译报错 [ app.json 文件内容错误] app.json: 在项目根目录未找到 app.json 解决办法&#xff1a; 微信开发者工具中打开设置->安全设置->打开服务端口用HBuilder X打开小程序文件夹&#xff0c;点击“运行到小程序模拟器”&#xff0c;生成配置文件&#xff0c;…...

MySQL 隔离级别:脏读、幻读及不可重复读的原理与示例

一、MySQL 隔离级别 MySQL 提供了四种隔离级别,用于控制事务之间的并发访问以及数据的可见性,不同隔离级别对脏读、幻读、不可重复读这几种并发数据问题有着不同的处理方式,具体如下: 隔离级别脏读不可重复读幻读性能特点及锁机制读未提交(READ UNCOMMITTED)允许出现允许…...

【项目实战】通过多模态+LangGraph实现PPT生成助手

PPT自动生成系统 基于LangGraph的PPT自动生成系统&#xff0c;可以将Markdown文档自动转换为PPT演示文稿。 功能特点 Markdown解析&#xff1a;自动解析Markdown文档结构PPT模板分析&#xff1a;分析PPT模板的布局和风格智能布局决策&#xff1a;匹配内容与合适的PPT布局自动…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

从零实现STL哈希容器:unordered_map/unordered_set封装详解

本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

Mac下Android Studio扫描根目录卡死问题记录

环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中&#xff0c;提示一个依赖外部头文件的cpp源文件需要同步&#xff0c;点…...

Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?

Redis 的发布订阅&#xff08;Pub/Sub&#xff09;模式与专业的 MQ&#xff08;Message Queue&#xff09;如 Kafka、RabbitMQ 进行比较&#xff0c;核心的权衡点在于&#xff1a;简单与速度 vs. 可靠与功能。 下面我们详细展开对比。 Redis Pub/Sub 的核心特点 它是一个发后…...

IP如何挑?2025年海外专线IP如何购买?

你花了时间和预算买了IP&#xff0c;结果IP质量不佳&#xff0c;项目效率低下不说&#xff0c;还可能带来莫名的网络问题&#xff0c;是不是太闹心了&#xff1f;尤其是在面对海外专线IP时&#xff0c;到底怎么才能买到适合自己的呢&#xff1f;所以&#xff0c;挑IP绝对是个技…...

[大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.

ollama官网: 下载 https://ollama.com/ 安装 查看可以使用的模型 https://ollama.com/search 例如 https://ollama.com/library/deepseek-r1/tags # deepseek-r1:7bollama pull deepseek-r1:7b改token数量为409622 16384 ollama命令说明 ollama serve #&#xff1a…...

根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的----NTFS源代码分析--重要

根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的 第一部分&#xff1a; 0: kd> g Breakpoint 9 hit Ntfs!ReadIndexBuffer: f7173886 55 push ebp 0: kd> kc # 00 Ntfs!ReadIndexBuffer 01 Ntfs!FindFirstIndexEntry 02 Ntfs!NtfsUpda…...

OD 算法题 B卷【正整数到Excel编号之间的转换】

文章目录 正整数到Excel编号之间的转换 正整数到Excel编号之间的转换 excel的列编号是这样的&#xff1a;a b c … z aa ab ac… az ba bb bc…yz za zb zc …zz aaa aab aac…; 分别代表以下的编号1 2 3 … 26 27 28 29… 52 53 54 55… 676 677 678 679 … 702 703 704 705;…...