加入收藏 | 设为首页 | 会员中心 | 我要投稿 财气旺网 - 财气网 (https://www.caiqiwang.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux:实现一个简单的线程池

发布时间:2022-11-02 14:02:33 所属栏目:Linux 来源:未知
导读: 线程池
一、定义:线程池是一种线程使用模式。
二、目的:线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免

线程池

一、定义:线程池是一种线程使用模式。

二、目的:线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务。这避免了在处理短时间任务时创建与销毁线程的代价。线程池不仅能够保证内核的充分利用线程池linux,还能防止过分调度。可用线程数量应该取决于可用的并发处理器、处理器内核、内存、网络sockets等的数量。

三、应用场景:

四、线程池示例:

功能:1. 创建固定数量线程池,循环从任务队列中获取任务对象,

2. 获取到任务对象后,执行任务对象中的任务接口

/* 线程池 */  
#ifndef __M_POOL_H_
#define __M_POOL_H_ 
#include
#include
#include
#include
#include
#include
#include
#define MAX_THREAD 5
#define MAX_QUEUE 10
typedef bool (*HandleFunc)(int data);
class MyTask
{
  private:
    int _data;
    HandleFunc _handleFunc;
  public:
    MyTask(){}
    MyTask(int data,HandleFunc handleFunc)
      :_data(data)
       ,_handleFunc(handleFunc) {}
    ~MyTask(){}
   void SetTask(int data,HandleFunc handleFunc)
   {
     _data = data;
     _handleFunc = handleFunc;
   }
   bool Run()
   {
     return _handleFunc(_data);
   }
};
class MyThreadPool
{
  private:
    int max_thread;   //线程池中最大线程数
    int cur_thread;   //当前线程池中的线程数
    int quitFlag;     //线程池中线程退出的标志
    int max_queue;    //队列的最大节点数
    std::queue task_list;    //任务队列
    pthread_mutex_t mutex;    //互斥量
    pthread_cond_t empty;     //条件变量
    pthread_cond_t full;      //条件变量
    void ThreadLock()       //加锁
    {
      pthread_mutex_lock(&mutex);
    } 
    void ThreadUnLock()     //解锁
    {
      pthread_mutex_unlock(&mutex);
    }
    void ConsumerWait()    //消费者等待(为空则等待)
    {
      if(quitFlag == true)  //所有任务均完成,则线程退出
      {
        cur_thread--;

线程池linux_c#线程池调用线程_linux epoll 线程池

pthread_mutex_unlock(&mutex); printf("Thread[%p] exited.\n",pthread_self()); pthread_exit(NULL); } pthread_cond_wait(&empty,&mutex); } void ConsumerWake() //唤醒消费者 { pthread_cond_signal(&empty); } void ConsumerWakeAll() //广播唤醒剩余所有线程 { pthread_cond_broadcast(&empty); } void ProducterWait() //生产者等待(满了则等待) { pthread_cond_wait(&full,&mutex); } void ProducterWake() //唤醒生产者 { pthread_cond_signal(&full); } bool QueueEmpty() { return task_list.empty(); } bool QueueFull() { return (task_list.size() == max_queue ? true : false); } void QueuePush(MyTask& task) //将task放入任务队列 { task_list.push(task); } void QueuePop(MyTask* task) //从任务队列取出task { *task = task_list.front(); task_list.pop(); } public: MyThreadPool(int maxt = MAX_THREAD,int maxq = MAX_QUEUE) :max_thread(maxt) ,max_queue(maxq) { cur_thread = maxt; quitFlag = false; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&empty,NULL); pthread_cond_init(&full,NULL); } ~MyThreadPool() { pthread_mutex_destroy(&mutex); pthread_cond_destroy(&empty); pthread_cond_destroy(&full); } static void* ThreadFunc(void* arg) //线程执行函数 { std::cout<<"In ThreadFunc()"<ThreadLock(); while(p->QueueEmpty()) //任务队列没任务 p->ConsumerWait(); MyTask task; p->QueuePop(&task); p->ProducterWake(); p->ThreadUnLock(); task.Run(); } return NULL; } bool MyThreadPool_Init() //初始化线程池 { pthread_t tid; int ret; for(int i = 0;i 0) //退出线程池内的所有线程 { ConsumerWakeAll(); usleep(3000); } } }; bool TaskHandle(int data) { srand((unsigned int)time(NULL)); int n = rand()%5; printf("Thread[%p] is ready to sleep for %d seconds.\n",pthread_self(),n); sleep(n); return true; } int main() { MyThreadPool p; p.MyThreadPool_Init(); MyTask task[10]; for(int i =0;i<10;++i) { task[i].SetTask(i,TaskHandle); p.AddTask(task[i]); printf("task[%d] : Add success!\n ",i); } p.MyThreadPool_Quit(); return 0; } #endif

linux epoll 线程池_c#线程池调用线程_线程池linux

linux epoll 线程池_c#线程池调用线程_线程池linux

(编辑:财气旺网 - 财气网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!