Linux-线程池

线程池

一、线程池概述

1.1 - 线程池概念

  • 线程池是预先创建线程的一种技术。线程池在任务还没有到来之前,创建一定数量(N)的线程(用来对执行任务),放入空闲队列中。这些线程都是处于阻塞(Suspended)状态,不消耗CPU,但占用较小的内存空间。
  • 当新任务到来时,缓冲池选择一个空闲线程,把任务传入此线程运行;如果缓冲池已经没有空闲线程,则新建若干个线程。当系统比较空闲时,大部分线程都一直处于暂停状态,线程池自动销毁一部分线程,回收系统资源。

1.2 - 线程池优势

创建多线程的高并发服务器(没有线程池时)

  • 每次收到一条消息都创建一个新的线程,要知道线程的资源对于系统来说是很昂贵的,消息处理完成还要销毁这个线程;
  • 这个程序用到的线程数量是没有限制的。当线程到达一定数量,程序反而因线程在cpu切换开销的原因处理效率降低。无论的你的服务器cpu是多少核心,这个现象都有发生的可能。
    线程多的问题该怎么解决呢,增加cpu核心数?治标不治本。对于开发者而言,最为常用也最为有效的是线程池化,也就是说线程池。

解决高并发问题(使用线程池)

  • 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。这避免了在处理短时间任务时创建与销毁线程的代价。线程池不仅能够保证内核的充分利用,还能防止过分调度。
  • 可用线程数量应该取决于可用的并发处理器、处理器内核、内存、网络sockets等的数量。例如,线程数一般取cpu数量+2比较合适,线程数过多会导致额外的线程切换开销。
  • 线程池其中一项很重要的技术点就是任务的队列,队列是一种采用的FIFO(first in first
    out)方式的线性表,也就是经常说的先进先出策略。队列虽然属于一种基础的数据结构,但是发挥了举足轻重的作用。

二、线程池工作原理

  • 线程池调度器包含创建空闲线程销毁空闲线程接口
  • 线程池调度器本身也是一个线程,主要负责任务调度与线程调度,其工作过程大致如下:
    • 从任务队列获取任务,如果队列为空,阻塞等待新任务到来

    • 队列不为空,取出该任务,从空闲线程队列取一线程,如果为空,判断工作者线程数是否达到上限,如果没有,则创建若个空闲线程,否则等待某一任务执行完毕,并且该任务对应的线程归还给线程池

    • 获得空闲工作者线程,将任务交给工作者线程来处理,工作者线程维护一任务指针,这里只要该指针指向任务,并且唤醒线程

    • 判断空闲工作者线程数是否超过最大工作者线程数,如果超过,销毁(空闲线程数-允许最大空闲线程数)个线程

  • 任务接口是一个抽象类,只有一个虚函数run方法,执行的是实际的业务逻辑
  • 工作者线程维护一任务指针,工作者线程的任务主要是运行任务对象的run方法
  • 当线程池调度器调度一个工作者线程后,就唤醒工作者线程,并调用run方法来执行实际的业务逻辑,当run方法执行完毕,即业务逻辑处理结束,将工作者线程归还到空闲线程池队列,而不是销毁。这样线程池调度器下一次就有机会调度到该工作者线程。

三、线程池示例代码

main.c

#include "threadpool.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

//往队列里面添加的任务
void* mytask(void *arg)
{
	printf("thread 0x%x is working on task %d\n", (int)pthread_self(), *(int*)arg);
	sleep(1);
	free(arg);
	return NULL;
}

int main(void)
{
	//创建线程池并初始化
	threadpool_t *pool;
	threadpool_init(&pool, 3);

	//往队列里面添加n=10个任务
	int i;
	for (i=0; i<10; i++)
	{
		int *arg = (int *)malloc(sizeof(int));
		*arg = i;
		threadpool_add_task(pool, mytask, arg);
	}

	//执行完所有任务销毁线程池
	threadpool_destroy(pool);
	
	return 0;
}

threadpool.h

#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_

#include "condition.h"

// 任务结构体,将任务放入队列由线程池中的线程来执行
typedef struct task
{
	void *(*run)(void *arg);	// 任务回调函数
	void *arg;					// 回调函数参数
	struct task *next;			// 链表队列
} task_t;

// 线程池结构体
typedef struct threadpool
{
	condition_t ready;		//任务准备就绪或者线程池销毁通知
	task_t *first;			//任务队列头指针
	task_t *last;			//任务队列尾指针
	int counter;			//线程池中当前线程数
	int idle;				//线程池中当前正在等待任务的线程数
	int max_threads;		//线程池中最大允许的线程数
	int quit;				//销毁线程池的时候置1
} threadpool_t;

// 初始化线程池
void threadpool_init(threadpool_t **pool, int threads);
// 往线程池中添加任务
void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg);
// 销毁线程池
void threadpool_destroy(threadpool_t *pool);

#endif /* _THREAD_POOL_H_ */

threadpool.c

#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>

//往线程池添加的线程,用来执行任务列表里面的任务
void *thread_routine(void *arg)
{
	struct timespec abstime;
	threadpool_t *pool = (threadpool_t *)arg;
	printf("thread 0x%x is starting\n", (int)pthread_self());

	//1. 设置为自分离线程
	pthread_detach(pthread_self());

	//3. 进入轮询工作模式,如果不退出且队列不为空则一直工作
	while(1)
	{
		//1. 先去看下有没有任务,有任务则处理任务,没有再wait
		
		//加锁,为了从任务队列中取出任务
		condition_lock(&pool->ready);
		printf("thread 0x%x is working\n", (int)pthread_self());
		
		if(pool->first != NULL)
		{
			task_t *t = pool->first; 	//取出第一个任务
			pool->first = t->next;   	//修改队列头
			condition_unlock(&pool->ready); //先解锁,提高效率

			//处理任务
			t->run(t->arg);
			free(t);
			continue; //既然本次有任务,可能下次还有任务,则继续查看是否有任务
		}
		else//没有任务则解锁
		{
			condition_unlock(&pool->ready);
		}
		//线程池被销毁则退出
		if(pool->quit)
		{
			break;
		}

		//2. 如果没有任务,则等待
		printf("thread 0x%x is waiting\n", (int)pthread_self());
		while(1)
		{
			clock_gettime(CLOCK_REALTIME, &abstime);
			abstime.tv_sec += 2; //延时2s
			
			condition_lock(&pool->ready);
			pool->idle++;//闲置数+1
			
			//等待新任务来临的通知
			int status = condition_timedwait(&pool->ready, &abstime);
			condition_unlock(&pool->ready);
			//有新任务来临
			if (status != ETIMEDOUT || pool->quit)
			{
				printf("thread 0x%x get signal\n", (int)pthread_self());
				break;
			}//没有新任务来
			else
			{
				printf("thread 0x%x is wait timed out\n", (int)pthread_self());
				if(pool->counter >= 3)//判断闲置的线程是否过多
				{
					goto THREAD_EXIT;//闲置的线程过多则销毁线程
				}
			}			
		}
	}
//闲置的线程过多则销毁线程
THREAD_EXIT:	
	printf("thread 0x%x is exit\n", (int)pthread_self());
	condition_lock(&pool->ready);
	pool->counter--;
	condition_unlock(&pool->ready);
	pthread_exit(NULL); //退出线程
}

void threadpool_init(threadpool_t **pool, int threads)
{
	//1. 初始化基本的线程池参数
	int i;
	threadpool_t *newpool = malloc(sizeof(threadpool_t));
	
	*pool = newpool;
	newpool->max_threads = threads;
	newpool->quit = 0;
	newpool->idle = 0; //??
	newpool->first = NULL;
	newpool->last = NULL;
	newpool->counter = 0;
	condition_init(&newpool->ready);

	//2. 默认有线程数,则在初始化的时候同时初始化N个线程
#if 1	
	for(i= 0; i < 5; i++)
	{
		pthread_t tid;
		if(pthread_create(&tid, NULL, thread_routine, newpool) == 0)//where is task?
		{
			condition_lock(&newpool->ready);
			newpool->counter++;
			condition_unlock(&newpool->ready);
		}
	}
#endif	
}

void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg)
{
	if(pool->quit)
		return;

	//1. 生成任务包
	task_t *task = malloc(sizeof(task_t));
	task->run = run;
	task->arg = arg;

	//2. 加到task队列, 先上锁,再添加,再解锁
	printf("Add new task %p ! \n", task);
	condition_lock(&pool->ready);
	if(pool->last == NULL)
	{
		pool->last = task; //队列头
		pool->first = pool->last; //初始化头
	}
	else
	{
		pool->last->next = task; // add
		pool->last = task;
	}

	//3. 计算一下线程数是否满足任务处理速度,不满足则创建一批
	if(pool->counter < pool->max_threads && pool->idle <= 0)
	{
		//??线程创建策略,根据实际环境选择
		//	策略1: 固定增长,每次增长??
		//	策略2: 指数增长,每次翻倍?? 也就是创建 pool->counter
		//	策略3: 线下增长,每次+1
		//  策略4: 根据任务数量增长
		pthread_t tid;
		if(pthread_create(&tid, NULL, thread_routine, pool) == 0) //where is task?
		{
			pool->counter++;
		}
	}

	//4. 通知线程去取任务处理
	if(pool->idle > 0)
	{
		condition_signal(&pool->ready); //唤醒一个线程去处理任务
	}

	//5. 解锁
	condition_unlock(&pool->ready);
}

void threadpool_destroy(threadpool_t *pool)
{
	//1. 设置退出条件
	pool->quit = 1;

	//2. 等待所有线程退出
	while(pool->counter > 0)
	{
		//3. 广播,通知所有线程退出
		condition_lock(&pool->ready);
		condition_broadcast(&pool->ready); //唤醒所有线程退出
		condition_unlock(&pool->ready);
		sleep(1);
	}

	//4. 销毁线程池对象
	free(pool);
}


condition.h

#ifndef _CONDITION_H_
#define _CONDITION_H_

#include <pthread.h>

typedef struct condition
{
	pthread_mutex_t pmutex;
	pthread_cond_t pcond;
} condition_t;

int condition_init(condition_t *cond);
int condition_lock(condition_t *cond);
int condition_unlock(condition_t *cond);
int condition_wait(condition_t *cond);
int condition_timedwait(condition_t *cond, const struct timespec *abstime);
int condition_signal(condition_t *cond);
int condition_broadcast(condition_t *cond);
int condition_destroy(condition_t *cond);

#endif /* _CONDITION_H_ */

condition.c

#include "condition.h"  

int condition_init(condition_t *cond)
{
	int status;
	if ((status = pthread_mutex_init(&cond->pmutex, NULL)))
		return status;

	if ((status = pthread_cond_init(&cond->pcond, NULL)))
		return status;

	return 0;
}

int condition_lock(condition_t *cond)
{
	return pthread_mutex_lock(&cond->pmutex); 
}

int condition_unlock(condition_t *cond)
{
	return pthread_mutex_unlock(&cond->pmutex);
}

int condition_wait(condition_t *cond)
{
	return pthread_cond_wait(&cond->pcond, &cond->pmutex);
}

int condition_timedwait(condition_t *cond, const struct timespec *abstime)
{
	return pthread_cond_timedwait(&cond->pcond, &cond->pmutex, abstime);
}

int condition_signal(condition_t *cond)
{
	return pthread_cond_signal(&cond->pcond);
}

int condition_broadcast(condition_t* cond)
{
	return pthread_cond_broadcast(&cond->pcond);
}

int condition_destroy(condition_t* cond)
{
	int status;
	if ((status = pthread_mutex_destroy(&cond->pmutex)))
		return status;

	if ((status = pthread_cond_destroy(&cond->pcond)))
		return status;

	return 0;
}

Makefile

.PHONY:clean
CC=gcc
CFLAGS=-Wall -g
ALL=main
all:$(ALL)
OBJS=threadpool.o main.o condition.o
.c.o:
	$(CC) $(CFLAGS) -c $<

main:$(OBJS)
	$(CC) $(CFLAGS) $^ -o $@ -lpthread -lrt

clean:
	rm -f $(ALL) *.o

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值