作者:刘少静mm_527 | 来源:互联网 | 2023-10-10 17:56
threadpool.h
#ifndef _THREADPOOL_
#define _THREADPOOL_#include
#include
#include
#include
#include
#include
//任务
typedef struct Task
{void*(*fun)(void*arg);void *arg;
}Task;//线程池结构
typedef struct ThreadPool
{Task *task;pthread_t *work_pthread;pthread_t *manger_work_pthread; //线程池管理线程pthread_cond_t isfull; //任务不满条件变量pthread_cond_t isemety; //任务不为空条件变量pthread_mutex_t mutexpool; //锁线程池的锁pthread_mutex_t mutexbusy; int min;int max;int busynum;int lifenum;int exitnum;int queue_front; //任务队列存标记int queue_rear; //任务队列取标记int queue_size; //当前任务数量int queue_max; //任务最大数量int showdownfalt; //线程池销毁标志}ThreadPool;#define PTHREAD_ADDNUM 2 //线程增加数量
#define PTHREAD_DESTORYNUM 2 //线程销毁数量//线程池创建
int createThreadPool(ThreadPool*pool,int min,int max,int queueMaxNum);//添加任务
int addTask(ThreadPool*pool,void*(*fun)(void*arg),void*arg);//销毁线程池
int destroyPthreadPool(ThreadPool*pool);//工作者回调函数
void* workfun(void*arg);
//管理者回调函数
void* mangerfun(void*arg);#endif