【学习点滴】linux下的线程池相关知识

 -_-

 首先,线程池是什么?顾名思义,就是把一堆开辟好的线程放在一个池子里统一管理,就是一个线程池。

 其次,为什么要用线程池,难道来一个请求给它申请一个线程,请求处理完了释放线程不行么?也行,但是如果创建线程和销毁线程的时间比线程处理请求的时间长,而且请求很多的情况下,我们的CPU资源都浪费在了创建和销毁线程上了,所以这种方法的效率比较低,于是,我们可以将若干已经创建完成的线程放在一起统一管理,如果来了一个请求,我们从线程池中取出一个线程来处理,处理完了放回池内等待下一个任务,线程池的好处是避免了繁琐的创建和结束线程的时间,有效的利用了CPU资源。

    线程池的工作流程:

线程池的任务就在于负责池子中线程的创建,销毁和任务处理参数传递、唤醒和等待。

1.      创建若干线程,置入线程池

2.      任务达到时,从线程池取空闲线程

3.      取得了空闲线程,立即进行任务处理

4.      否则新建一个线程,并置入线程池,执行3

5.      如果创建失败或者线程池已满,根据设计策略选择返回错误或将任务置入处理队列,等待处理

6.      销毁线程池
 

直接上代码了:

#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "threadpool.h"

#define DEFAULT_TIME 10 // ervery 10s check the task_queue and thread_status
#define MIN_WAIT_TASK_NUM 10 // if queue_size > MIN_WAIT_TASK_NUM, we need add thread
#define DEFAULT_THREAD_VARY 10 //# of thread num vary

typedef struct
{
	void *(*function)(void *);
	void *arg;
} threadpool_task_t;

struct threadpool_t
{
	pthread_mutex_t lock;// mutex for the taskpool
	pthread_mutex_t thread_counter;//mutex for count the busy thread
	pthread_cond_t queue_not_full;
	pthread_cond_t queue_not_empty;
	pthread_t *threads;
	pthread_t adjust_tid;
	threadpool_task_t *task_queue;
	int min_thr_num;
	int max_thr_num;
	int live_thr_num;
	int busy_thr_num;
	int wait_exit_thr_num;
	int queue_front;
	int queue_rear;
	int queue_size;
	int queue_max_size;
	bool shutdown;
};

/**
 * @function void *threadpool_thread(void *threadpool)
 * @desc the worker thread
 * @param threadpool the pool which own the thread
 */
void *threadpool_thread(void *threadpool);
/**
 * @function void *adjust_thread(void *threadpool);
 * @desc manager thread
 * @param threadpool the threadpool
 */
void *adjust_thread(void *threadpool);
/**
 * check a thread is alive
 */
bool is_thread_alive(pthread_t tid);

int threadpool_free(threadpool_t *pool);

threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size)
{
	threadpool_t *pool = NULL;
    do{
	    if((pool = (threadpool_t *)malloc(sizeof(threadpool_t))) == NULL)
	    {
		    printf("malloc threadpool fail");
		    break;
	    }

	    pool->min_thr_num = min_thr_num;
	    pool->max_thr_num = max_thr_num;
	    pool->busy_thr_num = 0;
	    pool->live_thr_num = min_thr_num;
	    pool->queue_size = 0;
	    pool->queue_max_size = queue_max_size;
	    pool->queue_front = 0;
	    pool->queue_rear = 0;
	    pool->shutdown = false;

	    pool->threads = (pthread_t *)malloc(sizeof(pthread_t)*max_thr_num);
	    if (pool->threads == NULL)
	    {
	    	printf("malloc threads fail");
	    	break;
	    }
	    memset(pool->threads, 0, sizeof(pool->threads));

	    pool->task_queue = (threadpool_task_t *)malloc(sizeof(threadpool_task_t)*queue_max_size);
	    if (pool->task_queue == NULL)
	    {
	    	printf("malloc task_queue fail");
	    	break;
	    }

	    if (pthread_mutex_init(&(pool->lock), NULL) != 0
	    	|| pthread_mutex_init(&(pool->thread_counter), NULL) != 0
	    	|| pthread_cond_init(&(pool->queue_not_empty), NULL) != 0
	    	|| pthread_cond_init(&(pool->queue_not_full), NULL) != 0)
	    {
	    	printf("init the lock or cond fail");
	    	break;
	    }

	    /**
	     * start work thread  min_thr_num
	     */
	    for (int i = 0; i < min_thr_num; i++)
	    {
	    	pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);
	    	printf("start thread 0x%x...\n", pool->threads[i]);
	    }
	    pthread_create(&(pool->adjust_tid), NULL, adjust_thread, (void *)pool);
	    return pool;

    }while(0);

    threadpool_free(pool);
	return NULL;
}

int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg)
{
	assert(pool != NULL);
	assert(function != NULL);
	assert(arg != NULL);

	pthread_mutex_lock(&(pool->lock));

	while ((pool->queue_size == pool->queue_max_size) && (!pool->shutdown))
	{
		//queue full  wait
		pthread_cond_wait(&(pool->queue_not_full), &(pool->lock));
	}
	if (pool->shutdown)
	{
		pthread_mutex_unlock(&(pool->lock));
	}

	//add a task to queue
	if (pool->task_queue[pool->queue_rear].arg != NULL)
	{
		free(pool->task_queue[pool->queue_rear].arg);
		pool->task_queue[pool->queue_rear].arg = NULL;
	}
	pool->task_queue[pool->queue_rear].function = function;
	pool->task_queue[pool->queue_rear].arg = arg;
	pool->queue_rear = (pool->queue_rear + 1)%pool->queue_max_size;
	pool->queue_size++;

	//queue not empty
	pthread_cond_signal(&(pool->queue_not_empty));
	pthread_mutex_unlock(&(pool->lock));

	return 0;
}

void *threadpool_thread(void *threadpool)
{
	threadpool_t *pool = (threadpool_t *)threadpool;
	threadpool_task_t task;
	while(true)
	{
		/* Lock must be taken to wait on conditional variable */
		pthread_mutex_lock(&(pool->lock));

		while ((pool->queue_size == 0) && (!pool->shutdown))
		{
			printf("thread 0x%x is waiting\n", pthread_self());
			pthread_cond_wait(&(pool->queue_not_empty), &(pool->lock));
			if (pool->wait_exit_thr_num > 0)
			{
				pool->wait_exit_thr_num--;
				if (pool->live_thr_num > pool->min_thr_num)
				{
					printf("thread 0x%x is exiting\n", pthread_self());
				    pool->live_thr_num--;
				    pthread_mutex_unlock(&(pool->lock));
				    pthread_exit(NULL);
				}
			}
		}

		if (pool->shutdown)
		{
			pthread_mutex_unlock(&(pool->lock));
			printf("thread 0x%x is exiting\n", pthread_self());
			pthread_exit(NULL);
		}
		//get a task from queue
		task.function = pool->task_queue[pool->queue_front].function;
		task.arg = pool->task_queue[pool->queue_front].arg;
		pool->queue_front = (pool->queue_front + 1)%pool->queue_max_size;
		pool->queue_size--;

		//now queue must be not full
		pthread_cond_broadcast(&(pool->queue_not_full));

		pthread_mutex_unlock(&(pool->lock));

		// Get to work
		printf("thread 0x%x start working\n", pthread_self());
		pthread_mutex_lock(&(pool->thread_counter));
		pool->busy_thr_num++;
		pthread_mutex_unlock(&(pool->thread_counter));
		(*(task.function))(task.arg);
		// task run over
		printf("thread 0x%x end working\n", pthread_self());
		pthread_mutex_lock(&(pool->thread_counter));
		pool->busy_thr_num--;
		pthread_mutex_unlock(&(pool->thread_counter));
	}

	pthread_exit(NULL);
	return (NULL);
}


void *adjust_thread(void *threadpool)
{
	threadpool_t *pool = (threadpool_t *)threadpool;
	while (!pool->shutdown)
	{
		sleep(DEFAULT_TIME);
		pthread_mutex_lock(&(pool->lock));
		int queue_size = pool->queue_size;
		int live_thr_num = pool->live_thr_num;
		pthread_mutex_unlock(&(pool->lock));

		pthread_mutex_lock(&(pool->thread_counter));
		int busy_thr_num = pool->busy_thr_num;
		pthread_mutex_unlock(&(pool->thread_counter));

		if (queue_size >= MIN_WAIT_TASK_NUM
				&& live_thr_num < pool->max_thr_num)
		{
			//need add thread
			pthread_mutex_lock(&(pool->lock));
			int add = 0;
			for (int i = 0; i < pool->max_thr_num && add < DEFAULT_THREAD_VARY
			&& pool->live_thr_num < pool->max_thr_num; i++)
			{
				if (pool->threads[i] == 0 || !is_thread_alive(pool->threads[i]))
				{
					pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);
					add++;
					pool->live_thr_num++;
				}
			}
			pthread_mutex_unlock(&(pool->lock));
		}

		if ((busy_thr_num * 2) < live_thr_num
				&& live_thr_num > pool->min_thr_num)
		{
			//need del thread
			pthread_mutex_lock(&(pool->lock));
			pool->wait_exit_thr_num = DEFAULT_THREAD_VARY;
			pthread_mutex_unlock(&(pool->lock));
			//wake up thread to exit
			for (int i = 0; i < DEFAULT_THREAD_VARY; i++)
			{
				pthread_cond_signal(&(pool->queue_not_empty));
			}
		}
	}
}

int threadpool_destroy(threadpool_t *pool)
{
	if (pool == NULL)
	{
		return -1;
	}

	pool->shutdown = true;
	//adjust_tid exit first
	pthread_join(pool->adjust_tid, NULL);
	// wake up the waiting thread
	pthread_cond_broadcast(&(pool->queue_not_empty));
	for (int i = 0; i < pool->min_thr_num; i++)
	{
		pthread_join(pool->threads[i], NULL);
	}
	threadpool_free(pool);
	return 0;
}

int threadpool_free(threadpool_t *pool)
{
	if (pool == NULL)
	{
		return -1;
	}
	if (pool->task_queue)
	{
		free(pool->task_queue);
	}
	if (pool->threads)
	{
		free(pool->threads);
		pthread_mutex_lock(&(pool->lock));
		pthread_mutex_destroy(&(pool->lock));
		pthread_mutex_lock(&(pool->thread_counter));
		pthread_mutex_destroy(&(pool->thread_counter));
		pthread_cond_destroy(&(pool->queue_not_empty));
		pthread_cond_destroy(&(pool->queue_not_full));
	}
	free(pool);
	pool = NULL;
	return 0;
}

int threadpool_all_threadnum(threadpool_t *pool)
{
	int all_threadnum = -1;
	pthread_mutex_lock(&(pool->lock));
	all_threadnum = pool->live_thr_num;
	pthread_mutex_unlock(&(pool->lock));
	return all_threadnum;
}

int threadpool_busy_threadnum(threadpool_t *pool)
{
	int busy_threadnum = -1;
	pthread_mutex_lock(&(pool->thread_counter));
	busy_threadnum = pool->busy_thr_num;
	pthread_mutex_unlock(&(pool->thread_counter));
	return busy_threadnum;
}

bool is_thread_alive(pthread_t tid)
{
	int kill_rc = pthread_kill(tid, 0);
	if (kill_rc == ESRCH)
	{
		return false;
	}
	return true;
}

// for test
//void *process(void *arg)
//{
//	printf("thread 0x%x working on task %d\n ",pthread_self(),*(int *)arg);
//	sleep(1);
//	printf("task %d is end\n",*(int *)arg);
//	return NULL;
//}
//int main()
//{
//	threadpool_t *thp = threadpool_create(3,100,12);
//	printf("pool inited");
//
//	int *num = (int *)malloc(sizeof(int)*20);
//	for (int i=0;i<10;i++)
//	{
//		num[i]=i;
//		printf("add task %d\n",i);
//		threadpool_add(thp,process,(void*)&num[i]);
//	}
//	sleep(10);
//	threadpool_destroy(thp);
//}

头文件:



#ifndef __THREADPOOL_H_
#define __THREADPOOL_H_

typedef struct threadpool_t threadpool_t;

/**
 * @function threadpool_create
 * @descCreates a threadpool_t object.
 * @param thr_num  thread num
 * @param max_thr_num  max thread size
 * @param queue_max_size   size of the queue.
 * @return a newly created thread pool or NULL
 */
threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size);

/**
 * @function threadpool_add
 * @desc add a new task in the queue of a thread pool
 * @param pool     Thread pool to which add the task.
 * @param function Pointer to the function that will perform the task.
 * @param argument Argument to be passed to the function.
 * @return 0 if all goes well,else -1
 */
int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg);

/**
 * @function threadpool_destroy
 * @desc Stops and destroys a thread pool.
 * @param pool  Thread pool to destroy.
 * @return 0 if destory success else -1
 */
int threadpool_destroy(threadpool_t *pool);

/**
 * @desc get the thread num
 * @pool pool threadpool
 * @return # of the thread
 */
int threadpool_all_threadnum(threadpool_t *pool);

/**
 * desc get the busy thread num
 * @param pool threadpool
 * return # of the busy thread
 */
int threadpool_busy_threadnum(threadpool_t *pool);

#endif

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值