这里参考知乎上一篇C语言线程池代码实现,来理解一下线程池。
线程池是一种实现程序并发执行的软件设计模式,主要是避免为了短时间处理的任务进行频繁的线程创建和销毁,以及系统中过多的线程导致过度调度,这些都是消耗CPU资源较多的操作。
线程池创建了一个线程集合,以及维护一个工作队列,当任务到来的时候,把任务加入到工作队列中来,然后通知线程池中的线程去工作队列中取任务执行;这是一种生产者-消费者模型,因此工作队列是需要进行互斥访问的,由于生产者需要通知消费者有新任务到来,所以需要使用到条件变量;
下面是源代码:完全照抄上面参考文章中的代码
tpool.h
#ifndef T_POOL
#define T_POOL
#include <pthread.h>
#include <ctype.h>
typedef struct tpool_work{ //线程工作任务
void* (*work_routine)(void*); //当前工作任务执行函数
void* args; //执行函数参数
struct tpool_work_t* next; //下一个工作任务
}tpool_work_t;
typedef struct tpool{ //线程池结构体
size_t shutdown; //is tpool shutdown or not, 1 ---> yes; 0 ---> no
size_t maxnum_thread; // maximum of threads
pthread_t *thread_id; // a array of threads
tpool_work_t* tpool_head; // tpool_work queue
pthread_cond_t queue_ready; // condition varaible
pthread_mutex_t queue_lock; // queue lock
}tpool_t;
//这里使用了条件变量queue_ready和互斥变量queue_lock
/***************************************************
*@brief:
* create thread pool
*@args:
* max_thread_num ---> maximum of threads
* pool ---> address of thread pool
*@return value:
* 0 ---> create thread pool successfully
* othres ---> create thread pool failed
***************************************************/
int create_tpool(tpool_t** pool,size_t max_thread_num);
/***************************************************
*@brief:
* destroy thread pool
*@args:
* pool ---> address of pool
***************************************************/
void destroy_tpool(tpool_t* pool);
/**************************************************
*@brief:
* add tasks to thread pool
*@args:
* pool ---> thread pool
* routine ---> entry function of each thread
* args ---> arguments
*@return value:
* 0 ---> add ok
* others ---> add failed
**************************************************/
int add_task_2_tpool(tpool_t* pool,void* (*routine)(void*),void* args);
#endif//tpool.h
tpool.c
#include "tpool.h"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//每个线程通过work_routine启动
static void* work_routine(void* args)
{
tpool_t* pool = (tpool_t*)args;
tpool_work_t* work = NULL;
//循环,不断的从任务队列中获取任务,当没有任务时,通过条件变量的通知
while(1){
pthread_mutex_lock(&pool->queue_lock);
while(!pool->tpool_head && !pool->shutdown){ // if there is no works and pool is not shutdown, it should be suspended for being awake
pthread_cond_wait(&pool->queue_ready,&pool->queue_lock);
}
if(pool->shutdown){
pthread_mutex_unlock(&pool->queue_lock);//pool shutdown,release the mutex and exit
pthread_exit(NULL);
}
/* tweak a work*/
work = pool->tpool_head;
pool->tpool_head = (tpool_work_t*)pool->tpool_head->next; //任务队列通过链表连接
pthread_mutex_unlock(&pool->queue_lock);
work->work_routine(work->args); //执行任务
free(work); //释放工作任务的资源
}
return NULL;
}
//创建线程池
int create_tpool(tpool_t** pool,size_t max_thread_num)
{
(*pool) = (tpool_t*)malloc(sizeof(tpool_t));
if(NULL == *pool){
printf("in %s,malloc tpool_t failed!,errno = %d,explain:%s\n",__func__,errno,strerror(errno));
exit(-1);
}
(*pool)->shutdown = 0;
(*pool)->maxnum_thread = max_thread_num;
(*pool)->thread_id = (pthread_t*)malloc(sizeof(pthread_t)*max_thread_num);
if((*pool)->thread_id == NULL){
printf("in %s,init thread id failed,errno = %d,explain:%s",__func__,errno,strerror(errno));
exit(-1);
}
(*pool)->tpool_head = NULL;
if(pthread_mutex_init(&((*pool)->queue_lock),NULL) != 0){
printf("in %s,initial mutex failed,errno = %d,explain:%s",__func__,errno,strerror(errno));
exit(-1);
}
if(pthread_cond_init(&((*pool)->queue_ready),NULL) != 0){
printf("in %s,initial condition variable failed,errno = %d,explain:%s",__func__,errno,strerror(errno));
exit(-1);
}
for(int i = 0; i < max_thread_num; i++){
if(pthread_create(&((*pool)->thread_id[i]),NULL,work_routine,(void*)(*pool)) != 0){
printf("pthread_create failed!\n");
exit(-1);
}
}
return 0;
}
//销毁线程池,
void destroy_tpool(tpool_t* pool)
{
tpool_work_t* tmp_work;
if(pool->shutdown){
return;
}
pool->shutdown = 1;
pthread_mutex_lock(&pool->queue_lock);
pthread_cond_broadcast(&pool->queue_ready); //唤醒全部等待的线程
pthread_mutex_unlock(&pool->queue_lock);
for(int i = 0; i < pool->maxnum_thread; i++){
pthread_join(pool->thread_id[i],NULL);
}
free(pool->thread_id);
while(pool->tpool_head){
tmp_work = pool->tpool_head;
pool->tpool_head = (tpool_work_t*)pool->tpool_head->next;
free(tmp_work);
}
pthread_mutex_destroy(&pool->queue_lock);
pthread_cond_destroy(&pool->queue_ready);
free(pool);
}
int add_task_2_tpool(tpool_t* pool,void* (*routine)(void*),void* args)
{
tpool_work_t* work,*member;
if(!routine){
printf("rontine is null!\n");
return -1;
}
work = (tpool_work_t*)malloc(sizeof(tpool_work_t));
if(!work){
printf("in %s,malloc work error!,errno = %d,explain:%s\n",__func__,errno,strerror(errno));
return -1;
}
work->work_routine = routine;
work->args = args;
work->next = NULL;
pthread_mutex_lock(&pool->queue_lock);
member = pool->tpool_head;
if(!member){
pool->tpool_head = work;
}
else{
while(member->next){
member = (tpool_work_t*)member->next;
}
member->next = work;
}
//notify the pool that new task arrived!
pthread_cond_signal(&pool->queue_ready);
pthread_mutex_unlock(&pool->queue_lock);
return 0;
}
demo.c
#include "tpool.h"
#include <stdio.h>
#include <unistd.h>
#include <time.h>
void* fun(void* args)
{
int thread = (int)args;
printf("running the thread of %d\n",thread);
return NULL;
}
int main(int argc, char* args[])
{
tpool_t* pool = NULL;
if(0 != create_tpool(&pool,5)){
printf("create_tpool failed!\n");
return -1;
}
for(int i = 0; i < 1000; i++){
add_task_2_tpool(pool,fun,(void*)i);
}
sleep(2);
destroy_tpool(pool);
return 0;
}