嵌入式开发第30天(线程池)

时间:2021-06-19 19:43:25

定义

        什么是线程池?简单点说,线程池就是有一堆已经创建好了的线程,初始它们都处于空闲等待状态,当有新的任务需要处理的时候,就从这个池子里面取一个空闲等待的线程来处理该任务,当处理完成了就再次把该线程放回池中,以供后面的任务使用。当池子里的线程全都处理忙碌状态时,线程池中没有可用的空闲等待线程,此时,根据需要选择创建一个新的线程并置入池中,或者通知任务线程池忙,稍后再试。

 

为什么要用线程池?

         我们说,线程的创建和销毁比之进程的创建和销毁是轻量级的,但是当我们的任务需要大量进行大量线程的创建和销毁操作时,这个消耗就会变成的相当大。比如,当你设计一个压力性能测试框架的时候,需要连续产生大量的并发操作,这个是时候,线程池就可以很好的帮上你的忙。线程池的好处就在于线程复用,一个任务处理完成后,当前线程可以直接处理下一个任务,而不是销毁后再创建,非常适用于连续产生大量并发任务的场合。

 

线程池工作原理 

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

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

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

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

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

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

6.      销毁线程池


Thread_pool.h

#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include <errno.h>
#include <pthread.h>

#define MAX_WAITING_TASKS	1000    //最大等待数
#define MAX_ACTIVE_THREADS	20	//最大运行线程数

struct task				//单向链表的任务节点  
{
	void *(*task)(void *arg);	//想要运行的函数   void *  可以返回任何格式的值
	void *arg;			//函数的参数

	struct task *next;	//指向下一个的节点的指针
};

typedef struct thread_pool//线程池结构体
{
	pthread_mutex_t lock;//互斥锁
	pthread_cond_t  cond;//线程间同步,一般和pthread_mutex_t一起使用,以防止出现逻辑错误
						//即如果单独使用条件变量,某些情况下(条件变量前后出现对共享变量的读写)会出现问题

	struct task *task_list;//初始化任务链表节点

	pthread_t *tids;//初始化线程

	unsigned waiting_tasks;//等待任务数量
	unsigned active_threads;		//正在执行线程数

	bool shutdown;				//进程结束全体,任务符号
}thread_pool;					


bool
init_pool(thread_pool *pool,
          unsigned int threads_number);		//初始化池
bool
add_task(thread_pool *pool,//添加任务
         void *(*task)(void *arg),
         void *arg);

int 
add_thread(thread_pool *pool,//增加线程
           unsigned int additional_threads_number);

int 
remove_thread(thread_pool *pool,		//去除线程
              unsigned int removing_threads_number);

bool destroy_pool(thread_pool *pool);		//销毁线程池
void *routine(void *arg);

#endif

#include "thread_pool.h"

void handler(void *arg)				//预防 线程在运行过程中 被remove掉而导致的死锁


	pthread_mutex_unlock((pthread_mutex_t *)arg);	解锁
}

void *routine(void *arg)//线程的任务
{
	thread_pool *pool = (thread_pool *)arg;	//初始化线程池节点
	struct task *p;				//初始化任何节点

	while(1)
	{

		pthread_cleanup_push(handler, (void *)&pool->lock);  
/*
pthread_cleanup_push  
work();
pthread_cleanup_pop
这两个是一对
他们防止死锁,因为若在运行work 的过程中,线程被摧毁了,他们就会调用 handle,然后解锁
*/ 
		pthread_mutex_lock(&pool->lock); //锁定
		while(pool->waiting_tasks == 0 && !pool->shutdown) //如果任务为空就进入条件变量 休眠模式
	{		
		pthread_cond_wait(&pool->cond, &pool->lock);
	}
		if(pool->waiting_tasks == 0 && pool->shutdown == true) //如果任务为空,任务全部完成,则推出线程
	{		pthread_mutex_unlock(&pool->lock);pthread_exit(NULL);}
			p = pool->task_list->next;//把节点插入单向链表
			pool->task_list->next = p->next;
			pool->waiting_tasks--;
			pthread_mutex_unlock(&pool->lock);//解锁pthread_cleanup_pop(0); //解除预防死锁功能
			pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);//进入不接受 任何删除线程的模式
			(p->task)(p->arg);//运行任务
			pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);//回到可接受模式
			free(p);//释放节点
	}
		pthread_exit(NULL);//退出线程
}

bool init_pool(thread_pool *pool, unsigned int threads_number) //初始化线程池
{	
	pthread_mutex_init(&pool->lock, NULL);
	pthread_cond_init(&pool->cond, NULL);
	pool->shutdown = false;
	pool->task_list = malloc(sizeof(struct task));
	pool->tids = malloc(sizeof(pthread_t) * MAX_ACTIVE_THREADS);
	
	if(pool->task_list == NULL || pool->tids == NULL)
	{
		perror("allocate memory error");
		return false;
	}
	pool->task_list->next = NULL;
	pool->waiting_tasks = 0;
	pool->active_threads = threads_number;
	int i;
	for(i=0; i<pool->active_threads; i++)
	{
		if(pthread_create(&((pool->tids)[i]), NULL,routine, (void *)pool) != 0)
		{
			perror("create threads error");
			return false;
		}
	}
	return true;
}
	
bool add_task(thread_pool *pool,void *(*task)(void *arg), void *arg)
{	
	struct task *new_task = malloc(sizeof(struct task));
	if(new_task == NULL)
	{
		perror("allocate memory error");
		return false;
	}
	
	new_task->task = task;
	new_task->arg = arg;
	new_task->next = NULL;
	pthread_mutex_lock(&pool->lock);
	if(pool->waiting_tasks >= MAX_WAITING_TASKS)
	{
		pthread_mutex_unlock(&pool->lock);
		fprintf(stderr, "too many tasks.\n");
		free(new_task);
		return false;
	}
	struct task *tmp = pool->task_list;
	while(tmp->next != NULL)
		tmp = tmp->next;
	tmp->next = new_task;
	pool->waiting_tasks++;
	pthread_mutex_unlock(&pool->lock);
	pthread_cond_signal(&pool->cond);
	return true;
}
int add_thread(thread_pool *pool, unsigned additional_threads)
{
	if(additional_threads == 0)
		return 0;
	unsigned total_threads = pool->active_threads + additional_threads;
	int i, actual_increment = 0;
	for(i = pool->active_threads;
	i < total_threads && i < MAX_ACTIVE_THREADS;i++)
	{
		if(pthread_create(&((pool->tids)[i]),NULL, routine, (void *)pool) != 0)
		{
			perror("add threads error");
			if(actual_increment == 0)
				return -1;
			
				break;
		}actual_increment++;
	}pool->active_threads += actual_increment;
	return actual_increment;
	}
	int remove_thread(thread_pool *pool, unsigned int removing_threads)
	{
		if(removing_threads == 0)return pool->active_threads;
		int remain_threads = pool->active_threads - removing_threads;
		remain_threads = remain_threads>0 ? remain_threads:1;
		int i;
		for(i=pool->active_threads-1; i>remain_threads-1; i--)
		{
			errno = pthread_cancel(pool->tids[i]);
			if(errno != 0)
				break;
		}
		if(i == pool->active_threads-1)
			return -1;
		else{
			pool->active_threads = i+1;
			return i+1;
			}
	}
	bool destroy_pool(thread_pool *pool)
	{
		pool->shutdown = true;
		pthread_cond_broadcast(&pool->cond);
		int i;
		for(i=0; i<pool->active_threads; i++)
		{
			errno = pthread_join(pool->tids[i], NULL);
			if(errno != 0)
			{
				printf("join tids[%d] error: %s\n",i, strerror(errno));
			}
			else
				printf("[%u] is joined\n", (unsigned)pool->tids[i]);
		}
		free(pool->task_list);
		free(pool->tids);
		free(pool);
		return true;
	}
main.c

#include "thread_pool.h"

void *mytask(void *arg)
{
	int n = (int)arg;

	printf("[%u][%s] ==> job will be done in %d sec...\n",
		(unsigned)pthread_self(), __FUNCTION__, n);

	sleep(n);

	printf("[%u][%s] ==> job done!\n",
		(unsigned)pthread_self(), __FUNCTION__);

	return NULL;
}

void *count_time(void *arg)
{
	int i = 0;
	while(1)
	{
		sleep(1);
		printf("sec: %d\n", ++i);
	}
}

int main(void)
{
	pthread_t a;
	pthread_create(&a, NULL, count_time, NULL);

	// 1, initialize the pool
	thread_pool *pool = malloc(sizeof(thread_pool));
	init_pool(pool, 2);

	// 2, throw tasks
	printf("throwing 3 tasks...\n");
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));

	// 3, check active threads number
	printf("current thread number: %d\n",
			remove_thread(pool, 0));
	sleep(9);

	// 4, throw tasks
	printf("throwing another 2 tasks...\n");
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));

	// 5, add threads
	add_thread(pool, 2);

	sleep(5);

	// 6, remove threads
	printf("remove 3 threads from the pool, "
	       "current thread number: %d\n",
			remove_thread(pool, 3));

	// 7, destroy the pool
	destroy_pool(pool);
	return 0;
}