线程池:线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动一条线程来执行这个任务,执行结束以后,该线程并不会死亡,而是再次返回线程池中成为空闲状态,等待执行下一个任务。
线程池的伸缩性对性能有较大的影响。
创建太多线程,将会浪费一定的资源,有些线程未被充分使用。
销毁太多线程,将导致之后浪费时间再次创建它们。
创建线程太慢,将会导致长时间的等待,性能变差。
销毁线程太慢,导致其它线程资源饥饿。
threadpool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "array_queue.h"
typedef struct ThreadPool
{
pthread_t* tid;
size_t thread_cnt;
void (*task)(void*);
ArrayQueue* queue;
pthread_mutex_t mutex;
pthread_cond_t full;
pthread_cond_t null;
}ThreadPool;
// 创建线程池
ThreadPool* tp_create(size_t cnt,size_t cal,void (*task)(void*));
// 销毁线程池
void tp_destory(ThreadPool* tp);
// 启动
void start(ThreadPool* tp);
// 生产
void tp_push(ThreadPool* tp,void* ptr);
// 消费
void* tp_pop(ThreadPool* tp);
#endif//THREADPOOL_H
threadpool.c
#include <stdlib.h>
#include <pthread.h>
#include "threadpool.h"
// 创建线程池
ThreadPool* tp_create(size_t cnt,size_t cal,void (*task)(void*))
{
ThreadPool* tp = malloc(sizeof(ThreadPool));
tp->thread_cnt = cnt;
tp->queue = create_queue(cal);
tp->tid = malloc(sizeof(pthread_t)*cnt);
tp->task = task;
pthread_mutex_init(&tp->mutex,NULL);
pthread_cond_init(&tp->full,NULL);
pthread_cond_init(&tp->null,NULL);
return tp;
}
// 销毁线程池
void tp_destory(ThreadPool* tp)
{
for(int i=0; i<tp->thread_cnt; i++)
{
pthread_cancel(tp->tid[i]);
}
free(tp->tid);
destory_queue(tp->queue);
free(tp);
}
void* run(void* arg)
{
ThreadPool* tp = arg;
for(;;)
{
void* ptr = tp_pop(tp);
tp->task(ptr);
}
}
// 启动
void start(ThreadPool* tp)
{
for(int i=0; i<tp->thread_cnt; i++)
{
pthread_create(&tp->tid[i],NULL,run,tp);
printf("启动线程:%lu\n",tp->tid[i]);
}
}
// 生产
void tp_push(ThreadPool* tp,void* ptr)
{
pthread_mutex_lock(&tp->mutex);
while(full_queue(tp->queue))
{
pthread_cond_wait(&tp->full,&tp->mutex);
}
printf("tp_push:%p\n",ptr);
push_queue(tp->queue,ptr);
pthread_mutex_unlock(&tp->mutex);
pthread_cond_signal(&tp->null);
}
// 消费
void* tp_pop(ThreadPool* tp)
{
pthread_mutex_lock(&tp->mutex);
while(empty_queue(tp->queue))
{
pthread_cond_wait(&tp->null,&tp->mutex);
}
void* ptr = pop_queue(tp->queue);
printf("tp_pop:%p\n",ptr);
pthread_mutex_unlock(&tp->mutex);
pthread_cond_signal(&tp->full);
return ptr;
}