Linux下线程池的代码

此博客仅为了存放代码。(Linux,加锁,线程池)

头文件:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <string.h>

#define	_PORT		8000
#define	_BUF_SIZE	1500
#define	_LISTEN		128
#define	TRUE	1
#define	FALSE	0
#define	CREATE_DES	10


/*Task Queue*/
typedef struct
{
	void * (*task)(void*);
	void * arg;
}task_t;
/*Thread pool*/
typedef struct
{
	pthread_mutex_t pool_lock;
	pthread_mutex_t arg_lock;
	pthread_cond_t not_full;
	pthread_cond_t not_empty;
	pthread_t * threads; //(pthread_t *)malloc(max*(pthread_t))
	pthread_t Manager_tid;
	task_t * task_queue;
	int pool_min;
	int pool_max;
	int alive;
	int busy;
	int queue_front;
	int queue_rear;
	int queue_max;
	int queue_size;
	int pool_shutDown;
	int wait;
}pool_t;

/*Function*/
//1.创建线程池
pool_t * Pool_Create(int , int ,int);
//2.生产者工作
int Pool_Add_TaskQueue(pool_t *,void*(*)(void * ),void * );
//3.消费者
int Pool_Def_Task(pool_t * ,)
	/*
		上锁
		如果当前队列任务为0,并且线程池开关未关闭,消费者阻塞
		如果线程池开关为关闭,销毁线程池
		判断wait 回收数量如果大于0,结束自身线程,wait--;
		从头索引出队
		重新计算头索引 pool->queue_front = (pool->queue_front+1)%pool->queue_max;
		当前队列任务数减1 ,pool->queue_size--;
		唤醒一个生产者 pthread_cond_signal(&pool->not_full);
		执行用户工作
		解锁
		

	*/
//4.用户工作
//5.管理者工作
	/*
			周期性检测线程池情况,统计线程池数据
			试探存活线程数
			计算扩容与缩减
			
		
	*/

.c文件:

#include <thread_pool.h>
pool_t * Pool_Create(int thread_min , int thread_max , int queue_max)
{
		pool_t * pool = NULL;
		int i=0;
		if((pool = (pool_t * )malloc(sizeof(pool_t)))==NULL)
		{
				perror("Pool_Create() pool Malloc:");
				return NULL;
		}
		pool->pool_min = thread_min;
		pool->pool_max = thread_max;
		pool->alive = 0;
		pool->busy = 0;
		pool->queue_front = 0;
		pool->queue_rear = 0;
		pool->queue_max = queue_max;
		pool->queue_size = 0;
		pool->pool_shutDown = TRUE;
		if((pool->threads = (pthread_t *)malloc(thread_max * sizeof(pthread_t)))==NULL)
		{
				perror("Pool_Create() threads Malloc:");
				return NULL;
		}
		memset(pool->threads,0,thread_max * sizeof(pthread_t));
		if((pool->task_queue = (task_t *)malloc(queue_max * sizeof(task_t)))==NULL)
		{
				perror("Pool_Create() task_queue Malloc:");
				return NULL;
		}
		if(pthread_mutex_init(&pool->pool_lock,NULL)!=0||pthread_mutex_init(&pool->arg_lock,NULL)!=0||pthread_cond_init(&pool->not_full)!=0||pthread_cond_init(&pool->not_empty)!=0)
		{
				perror("Pool_Create() init Mutex or Cond:");
				return NULL;
		}
		for(i;i<CREATE_DES;i++){
				pthread_create(&pool->threads[i],NULL,(消费者工作),(传参));
		}
		pthread_create(&pool->Manager_tid,NULL,(管理者工作),(传参));
		return pool;
}
int Pool_Add_TaskQueue(pool_t * pool,void * (*Work)(void*arg),void*arg)
{
		//1.判断生产者是否需要阻塞
		pthread_mutex_lock(pool->pool_lock);
		while(pool->queue_size == pool->queue_max && pool->pool_shutDwon==TRUE){
				pthread_cond_wait(&pool->not_full,&pool->pool_lock);

		}
		if(pool->pool_shutDown == FALSE){
						pthread_mutex_unlock(&pool->pool_lock);
						/*退出处理*/
		}
		/*不一定行不行?*/
		if(pool->task_queue[pool->queue_rear].arg != NULL){
			free(pool->task_queue[pool->queue_rear].arg);
			pool->task_queue[pool->queue_rear] = NULL;
		}
		/*将任务添加到队列尾端*/
		pool->task_queue[pool->queue_rear].task = Work;
		pool->task_queue[pool->queue_rear].arg = arg;
		/*计算队列*/
		pool->queue_rear = (pool->queue_rear+1)%pool->queue_max;
		pool->queue_size++;
		/*唤醒一个消费者*/
		pthread_cond_signal(&pool->not_empty);
		/*解锁*/
		pthread_mutex_unlock(&pool->pool_lock);
		return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值