简易线程池实现

简易线程池实现

ThreadPool.hpp(线程池)

#ifndef THREADPOOL_HPP
#define THREADPOOL_HPP#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <vector>
#include <queue>
#include "sem.hpp"
#include "Mutex.hpp"
#include "Cond.hpp"
#include "thread.hpp"
#include "Task.hpp"
using namespace std;const int g_thread_num = 3;template <class T>
class ThreadPool
{
public:ThreadPool(int num = g_thread_num):num_(num){for(int i=1;i<=num;++i){threads_.push_back(new Thread(i,routine,(void*)this));}}static void* routine(void* args){ThreadData* td = (ThreadData*)args;ThreadPool<T>* tp = (ThreadPool<T>*)td->args_;while(true){T task;{lock_Guard lg(tp->getMutex());while(tp->isEmpty()) tp->waitcond(lg);task = tp->getTask();//tp->cv_.notify_one();}task();}}void run(){for(auto iter : threads_){iter->start();}}void pushTask(const T& task){sleep(2);lock_Guard lg(mtx_);// while(task_queue_.size() >= num_)// {//     cv_.Wait(lg);// }task_queue_.push(task);cout<<"push: "<<task.x_<<" + "<<task.y_<<" = ?"<<endl;cv_.notify_one();//sleep(1);}T getTask(){T t = task_queue_.front();task_queue_.pop();return t;}~ThreadPool(){for(auto iter : threads_){iter->join();delete iter;}}Mutex& getMutex() {return mtx_;}bool isEmpty() {return task_queue_.empty();}void waitcond(lock_Guard& lg) {cv_.Wait(lg);}
private:vector<Thread*> threads_;int num_;queue<T> task_queue_;Mutex mtx_;Condition_variable cv_;
};
#endif

thread.hpp(封装线程)

#ifndef THREAD_HPP
#define THREAD_HPP
#include<iostream>
#include<string>
#include<functional>
#include"ThreadPool.hpp"
using namespace std;typedef void*(*fun_t)(void*);
class ThreadData
{
public:void* args_;string name_;
};
class Thread
{
public:Thread(int num,fun_t callback,void* args):func_(callback){char namebuffer[64];snprintf(namebuffer,sizeof namebuffer,"thread-%d",num);tdata_.name_ = namebuffer;tdata_.args_ = args;}void start(){pthread_create(&tid_,nullptr,func_,(void*)&tdata_);}void join(){pthread_join(tid_,nullptr);}~Thread(){}
private:pthread_t tid_;fun_t func_;ThreadData tdata_;
};
#endif

Task.hpp(任务)

#ifndef TASK_HPP
#define TASK_HPP
#include<iostream>
class Task
{
public:Task() {}Task(int x, int y): x_(x), y_(y){}int operator()(){std::cout<<"recive and do: "<<x_<<" + "<<y_<<" = "<< x_ + y_<<std::endl;return x_ + y_;}int x_;int y_;
};#endif

Mutex.hpp(封装锁)

#include <iostream>
#include <cstdlib>
#ifndef MUTEX_HPP
#define MUTEX_HPP
class Mutex
{
public://Mutex(pthread_mutex_t* mutex)//:mutex_(mutex)Mutex(){pthread_mutex_init(&mutex_, nullptr);}void lock(){pthread_mutex_lock(&mutex_);}void unlock(){pthread_mutex_unlock(&mutex_);}pthread_mutex_t* getMutex(){return &mutex_;}~Mutex(){pthread_mutex_destroy(&mutex_);}Mutex(const Mutex& mtx) = delete;Mutex& operator=(const Mutex& mtx) = delete;
private:pthread_mutex_t mutex_;
};class lock_Guard
{
public:lock_Guard(Mutex& mutex):mutex_(mutex){mutex_.lock();}~lock_Guard(){mutex_.unlock();}pthread_mutex_t* getMutex(){return mutex_.getMutex();}lock_Guard(const lock_Guard& lg) = delete;lock_Guard& operator=(const lock_Guard& lg) = delete;
private:Mutex& mutex_;
};
#endif

Cond.hpp(封装条件变量)

#include <iostream>
#include <cstdlib>
#include "Mutex.hpp"
class Condition_variable
{
public:Condition_variable(){pthread_cond_init(&cond_,nullptr);}void Wait(lock_Guard& lg){pthread_cond_wait(&cond_,lg.getMutex());}void notify_one(){pthread_cond_signal(&cond_);}void notify_all(){pthread_cond_broadcast(&cond_);}~Condition_variable(){pthread_cond_destroy(&cond_);}
private:pthread_cond_t cond_;
};

sem.hpp(封装信号量)

#include <iostream>
#include <semaphore.h>
using namespace std;
class Sem
{
public:Sem(int value){sem_init(&sem_,0,value);}void P(){sem_wait(&sem_);}void V(){sem_post(&sem_);}~Sem(){sem_destroy(&sem_);}
private:sem_t sem_;
};

main.cc

#include "ThreadPool.hpp"
#include <pthread.h>
#define CUSTOMERSIZE 2
#define PRODUCTORSIZE 1int main()
{srand((unsigned long)time(nullptr) ^ getpid());ThreadPool<Task> *tp = new ThreadPool<Task>();tp->run();while(true){int x = rand()%100+1;int y = rand()%100+1;Task t(x,y);tp->pushTask(t);}return 0;
}

makefile

threadpool:main.ccg++ -o $@ $^ -std=c++11 -l pthread
.PHONY:clean
clean:rm -f threadpool

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

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

相关文章

【已解决】java: 无效的目标发行版: 19

问题描述 现在从Spring Boot官网下载的Spring boot文件的诸多配置的版本&#xff0c;无法直接选择和电脑已有配置相匹配的。所以直接下载安装包&#xff0c;并用IDEA打开后无法直接运行。 我在网站上下载的配置如下图&#xff1a; 我遇到的问题是运行时报错java: 无效的目标发…

Flink Checkpoint 状态后端详解:类型、特性对比及场景化选型指南

Apache Flink 提供了多种状态后端以支持 Checkpoint 机制下的状态持久化&#xff0c;确保在故障发生时能够快速恢复状态并实现 Exactly-Once 处理语义。以下是几种常见状态后端的详细介绍及其对比情况&#xff0c;以及不同场景下的选型建议&#xff1a; 1. MemoryStateBackend…

Go项目结构整洁实现|GitHub 3.5k

一、前言 hi&#xff0c;大家好&#xff0c;这里是白泽。今天给大家分享一个GitHub &#x1f31f; 3.5k 的 Go项目&#xff1a;go-backend-clean-arch https://github.com/amitshekhariitbhu/go-backend-clean-architecture 这个项目是一位老外写的&#xff0c;通过一个 HTT…

【QT学习】4.浮动窗口

结果&#xff1a; 代码&#xff1a; //制作核心控件&#xff1a;文本编辑框QTextEdit* pTextEditnew QTextEdit;//制作浮动控件connect(pMenu1,&QMenu::triggered,[](QAction* pAction){qDebug()<<pAction->text()<<endl;if(pAction->text()"浮动…

WebGIS 之 Openlayer

1.导入第三方依赖 <link rel"stylesheet" href"https://lib.baomitu.com/ol3/4.6.5/ol.css"> <script src"https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>2.初始化地图 初始化地图new ol.Map({}) 参数target:制定初始化…

国资委确定首批起航企业,重点布局人工智能、量子信息等新兴领域

国务院国资委近日按照“四新”&#xff08;新赛道、新技术、新平台、新机制&#xff09;标准&#xff0c;遴选确定了首批启航企业&#xff0c;加快新领域新赛道布局、培育发展新质生产力。 据了解&#xff0c;去年以来&#xff0c;国务院国资委围绕加快培育创新型国有企业&…

【华为OD机试C++】求int型正整数在内存中存储时1的个数

《最新华为OD机试题目带答案解析》:最新华为OD机试题目带答案解析,语言包括C、C++、Python、Java、JavaScript等。订阅专栏,获取专栏内所有文章阅读权限,持续同步更新! 文章目录 描述输入描述输出描述示例1示例2代码描述 输入一个 int 型的正整数,计算出该 int 型数据在内…

汉语语音基本特性

发音的生理基础和过程 人的发音生理机构如图 2.3.1所示,发音时由肺部收缩送出一股直流空气,经气管流至喉头声门处(声门即声带开口处),在发声之初,声门处的声带肌肉收缩,声带并拢间隙小于 1mm,这股直流空气冲过很小的缝隙,使声带得到横向和纵向的速度,此时,声带向两边运动,缝隙…

【LeetCode热题100】【链表】K 个一组翻转链表

题目链接&#xff1a;25. K 个一组翻转链表 - 力扣&#xff08;LeetCode&#xff09; 递归迭代&#xff0c;迭代翻转每组的链表节点&#xff0c;递归翻转下一组的链表节点 class Solution { public:ListNode* reverseKGroup(ListNode* head, int k) {ListNode*nextHeadhead; …

事件队列事件循环(EventLoop) 宏任务 微任务详解 面试题

事件队列 事件循环 EventLoop 宏任务 微任务详解 一、概念二、宏任务&#xff08;多个&#xff09;、微任务&#xff08;1个&#xff09;三、Promise 的构造函数四、process.nextTick在事件循环中的处理五、vue nextTick原理 一、概念 event: 事件 loop: 循环&#xff0c;循环…

突破编程_C++_C++14新特性(C++14新特性概览)

1 C14 的发展背景 C14 是 C 编程语言的一个重要版本&#xff0c;它的发展背景紧密关联于C语言的发展历程以及计算机科学领域的整体进步。 首先&#xff0c;C 语言起源于 20 世纪 80 年代早期&#xff0c;它的设计初衷是为了提供一种功能强大、高效且可移植的编程语言&#xf…

系统学习Docker:1_Docker简介以及2_安装Docker

01-Docker简介 什么是Docker 在不同的机器和操作系统中安装运行环境和依赖库是一件很烦人的事情&#xff0c;容器就是为了解决这一问题而出现的技术。 容器是一种虚拟化技术&#xff0c;将应用程序及其依赖项&#xff08;环境、系统工具等&#xff09;打包到一个独立的可移植…

数据结构:链表的双指针技巧

文章目录 一、链表相交问题二、单链表判环问题三、回文链表四、重排链表结点 初学双指针的同学&#xff0c;请先弄懂删除链表的倒数第 N 个结点。 并且在学习这一节时&#xff0c;不要将思维固化&#xff0c;认为只能这样做&#xff0c;这里的做法只是技巧。 一、链表相交问题 …

[Linux]基础IO(中)---理解重定向与系统调用dup2的使用、缓冲区的意义

重定向理解 在Linux下&#xff0c;当打开一个文件时&#xff0c;进程会遍历文件描述符表&#xff0c;找到当前没有被使用的 最小的一个下标&#xff0c;作为新的文件描述符。 代码验证&#xff1a; ①&#xff1a;先关闭下标为0的文件&#xff0c;在打开一个文件&#xff0c;…

图神经网络GNN

图神经网络GNN B、C、D的特征在某种程度上可以代表A的特征 上面就是一次GCN的操作

整型之韵,数之舞:大小端与浮点数的内存之旅

✨✨欢迎&#x1f44d;&#x1f44d;点赞☕️☕️收藏✍✍评论 个人主页&#xff1a;秋邱’博客 所属栏目&#xff1a;人工智能 &#xff08;感谢您的光临&#xff0c;您的光临蓬荜生辉&#xff09; 1.0 整形提升 我们先来看看代码。 int main() {char a 3;char b 127;char …

vue3父子组件之间的传值方式

在vue3中&#xff0c;可以使用props和emit来实现父子组件之间的通信。子组件可以使用emit发出一个事件&#xff0c;父组件监听这个事件&#xff0c;并将数据传递给另一个子组件。 下面的简单例子中&#xff0c;ChildA组件有一个按钮&#xff0c;当按钮被点击时&#xff0c;会触…

WebKit结构简介

WebKit是一款开源的浏览器引擎&#xff0c;用于渲染网页内容。它负责将HTML、CSS和JavaScript等网络资源转换为用户在屏幕上看到的图形界面。WebKit是一个跨平台的引擎&#xff0c;可以在多种操作系统上运行&#xff0c;如Windows、macOS、Linux等。 以下是一篇关于WebKit结构…

信创咨询岗位需求分析

岗位职责 1.深入了解国内IT解决方案市场&#xff0c;熟悉不同领域的国产产品&#xff0c;包括但不限于云计算、大数据、人工智能等。 2.跟踪国内科技发展趋势&#xff0c;了解最新的国产技术和产品&#xff0c;以为客户提供最适合的解决方案。 3.能够撰写清晰、详细的技术文档&…

浅谈iOS开发中的自动引用计数ARC

1.ARC是什么 我们知道&#xff0c;在C语言中&#xff0c;创建对象时必须手动分配和释放适量的内存。然而&#xff0c;在 Swift 中&#xff0c;当不再需要类实例时&#xff0c;ARC 会自动释放这些实例的内存。 Swift 使用 ARC 来跟踪和管理应用程序的内存&#xff0c;其主要是由…