带着疑问学习才能真正理解,关于线程池有如下疑问:
1.什么是线程池
2.为什么要使用线程池
3.线程池适合添加什么线程
4.线程池该如何实现
1.什么是线程池
线程池就是一个池子,池子里可以存放很多需要执行的线程;
创建特定数量的线程(这里称为执行线程)用于执行添加进池子的线程(这里称为任务);
添加进线程池的线程线存放在队列中,如果有空闲的执行线程,会被取走执行;如果所有的执行线程都在忙(线程池满),则会放在队列中等待,直到有空闲执行线程来取;
如果队列中没有需要执行的任务,执行线程就会挂起,等待新的任务加入;
2.为什么要使用线程池
在某些项目中可能会遇到频繁创建,销毁线程的情况;即某个线程创建后,运行一段逻辑后,线程结束,销毁线程;然后又有很多其他线程这样操作,这样不断地创建和销毁线程很费资源;
那么为了提高程序效率该怎么解决呢?
1.如果不要求并发性,就直接把线程做成一个进程接口函数,在适当的时候调用,执行完后就返回;
2.如果要求并发性,异步性,那么就需要运用线程,但是为了效率,就需要使用线程池来管理这些线程;
3.线程池适合添加什么线程
不知道在查阅线程池资料的时候有没有发现,基本上线程池demo都是以可以返回的线程举例;而且销毁线程池时都会执行pthread_join函数等待所有线程结束才进行进一步是资源释放?
有没有考虑过如果添加的线程是无限循环线程(无法返回的线程)那岂不是销毁线程池时会在pthread_join函数这一直阻塞无法继续向下运行?
可以返回的线程:
void *pthread_func(void *arg)
{
printf("this is pthread!\n");
return 0;
}
无限循环的线程:
void *pthread_func(void *arg)
{
while(true)
{
printf("this is pthread!\n");
sleep(1);
}
return 0;
}
其实,无限循环的线程基本不会添加到线程池中,线程池就是为了缓解频繁创建,销毁线程占用大量资源而创建的,而无限循环的线程不存在频繁创建和销毁所以就不需要线程池;
如果把无限循环的线程添加到线程池,会一直占用线程池的执行线程,导致这个执行线程不能处理其他任务造成资源浪费;也会导致线程池无法正常关闭或回收资源。
一直占用线程池的执行线程和独立的线程有啥区别?所以,无限循环的线程基本都是创建一个独立的线程进行线程处理;放在线程池中没意义反而浪费资源;
4.线程池该如何实现
实现线程池不同的人有不同的实现方法,但是流程基本是一样的,首先需要实现两个结构体:
任务信息结构体
typedef struct task{
void (*function) (void *);//函数指针,指向线程入口
void *arg; //线程参数
struct task *next; //指向下一个节点
}task;
线程池信息结构体
//线程池结构
typedef struct threadpool{
pthread_mutex_t mutex;//互斥锁
pthread_cond_t cond;//条件变量
task *task_head;//等待任务头节点
pthread_t *pthId;//执行线程pid
int max_thread_count;//最大执行线程数
int active_count;//活跃的执行线程数
int shutdown;//销毁线程池标志
}threadpool;
任务信息结构体,有人喜欢用数组,有人喜欢用链表,由于链表便于操作,我更喜欢用链表;
线程池信息结构体,这是最基本的信息,如果实际项目中需要其他信息可以进行扩展;
接下来对线程池的主要接口函数进行讲解:
创建线程池
// 初始化线程池
bool threadpool_create(threadpool **tp, int thread_count)
{
//tp:线程池对象指针,将创建的线程池指针返回给用户操作
//thread_count:线程池执行线程数量,要创建多少个线程执行任务
threadpool *pool = (threadpool *)malloc(sizeof(threadpool));
pthread_mutex_init(&pool->mutex, NULL);
pthread_cond_init(&pool->cond, NULL);
pool->task_head = NULL;
pool->max_thread_count = thread_count;
pool->active_count = 0;
pool->shutdown = 0;
//创建执行线程
pool->pthId = (pthread_t *)malloc(sizeof(pthread_t) * thread_count);
for (int i = 0; i < thread_count; i++)
{
if (pthread_create(&pool->pthId[i], NULL, threadpool_thread, pool))
return false;
}
(*tp) = pool;
return true;
}
初始化线程池信息结构体,创建执行数量的线程用于执行队列中的任务;
执行线程
void *threadpool_thread(void *arg)
{
threadpool *tp = (threadpool *)arg;
while (1)
{
pthread_mutex_lock(&tp->mutex);
//如果队列中没有任务时挂起线程
while (tp->task_head == NULL && !tp->shutdown)
{
pthread_cond_wait(&tp->cond, &tp->mutex);
}
//如果已经销毁线程池,结束线程
if (tp->shutdown)
{
pthread_mutex_unlock(&tp->mutex);
break;
}
//如果队列中有任务,有队列中获取任务并执行
task *new_task = tp->task_head;
if (new_task->next != NULL)
{
tp->task_head = new_task->next;
}
pthread_mutex_unlock(&tp->mutex);
// 执行任务
new_task->function(new_task->arg);
tp->active_count--;
free(new_task);
}
// 退出线程
pthread_exit(NULL);
}
添加任务到线程池
// 添加任务到线程池
void threadpool_add_task(threadpool *tp, void (*function)(void *), void *arg)
{
//如果线程池满,告诉用户,已添加的任务需要等待
if (tp->active_count == tp->max_thread_count)
{
printf("pthread pool is full ! wait a moment ... \n");
}
if (tp->shutdown)
{
printf("pthread pool is shutdown !\n");
return;
}
pthread_mutex_lock(&tp->mutex);
//将任务信息赋值非任务信息结构体
task *new_task = malloc(sizeof(task));
new_task->function = function;
new_task->arg = arg;
new_task->next = NULL;
//将任务添加到队列
if (tp->task_head == NULL)
{
tp->task_head = new_task;
}
else
{
task *tmp = tp->task_head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = new_task;
}
tp->active_count++;
pthread_cond_signal(&tp->cond); // 唤醒一个等待的线程
pthread_mutex_unlock(&tp->mutex);
return;
}
销毁线程池
// 线程池关闭
void threadpool_destroy(threadpool *tp)
{
pthread_mutex_lock(&tp->mutex);
if (tp->shutdown)
return -1;
tp->shutdown = 1;
pthread_cond_broadcast(&tp->cond); // 唤醒所有等待的线程
//等待线程执行完毕
for (int i = 0; i < tp->max_thread_count; i++)
{
pthread_join(tp->pthId[i], NULL);
}
free(tp->pthId);
tp->pthId = NULL;
task *head = NULL;
//将队列中未执行的任务销毁
while (tp->task_head->next != NULL)
{
head = tp->task_head;
tp->task_head = tp->task_head->next;
free(head);
head = NULL;
}
pthread_mutex_unlock(&tp->mutex);
pthread_mutex_destroy(&tp->mutex);
pthread_cond_destroy(&tp->cond);
free(tp);
tp = NULL;
return;
}
其他函数
//获取正在活跃的执行线程数量
int get_active_count(threadpool *tp)
{
return tp->active_count;
}
//获取线程池最大线程数量
int get_max_thread_count(threadpool *tp)
{
return tp->max_thread_count;
}
//获取空闲执行线程数量
int get_free_count(threadpool *tp)
{
return tp->max_thread_count - tp->active_count;
}
main函数
void func1(void)
{
printf("this is func 1!\n");
sleep(3);
}
void func2(void)
{
while (1)
{
printf("this is func 2!\n");
sleep(3);
}
}
void func3(void)
{
while (1)
{
printf("this is func 3!\n");
sleep(3);
}
}
int main(void)
{
threadpool *pool = NULL;
bool ret = false;
ret = threadpool_create(&pool, 2);
if (ret == false)
{
printf("create thread pool error!\n");
return -1;
}
threadpool_add_task(pool, func1, NULL);
threadpool_add_task(pool, func2, NULL);
threadpool_add_task(pool, func3, NULL);
sleep(10);
printf("end pthread!\n");
threadpool_destroy(pool);
printf("end ok!\n");
return 0;
}