线程池:一般线程是系统在接到任务的时候创建,在任务结束的时候被销毁。如果有很多短小大量的任务,那么一条条创建和销毁线程就显得非常浪费资源。根据这种需求,如果将定量的线程创建好后等待任务,任务来之后线程被唤醒,执行完之后又沉睡,那么就只会消耗少量的系统资源。
任务节点:
//在threadpool.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; //退出信号
}threadpool_t;
condition.h
typedef struct condition
{
pthread_mutex_t mutex;
pthread_cond_t cond;
}condition_t;
此图片源自http://www.cnblogs.com/coser/archive/2012/03/10/2389264.html
首先初始化线程池
增加task节点
初始化任务节点 (加阻塞mutex)
判断任务链表是否存在任务
判断空闲线程数量
判断当前线程数量是否达到最大值(唤醒或创建)
(唤醒) pthread_signal
(创建)调用pthread_create函数创建线程(参数传rount函数地址)
解锁mutex
rount函数(线程执行函数)
初始化pool对象
判断是否有任务或超时等待
取任务执行(跑run函数)
等待线程销毁通知
超时处理
#include "condition.h"
void condition_init(condition_t *cond)
{
pthread_mutex_init(&cond->mutex, NULL);
pthread_cond_init(&cond->cond, NULL);
}
void condition_lock(condition_t *cond)
{
pthread_mutex_lock(&cond->mutex);
}
void condition_unlock(condition_t *cond)
{
pthread_mutex_unlock(&cond->mutex);
}
void condition_wait(condition_t *cond)
{
pthread_cond_wait(&cond->cond, &cond->mutex);
}
int condition_timedwait(condition_t *cond, const struct timespec *abstime)
{
return pthread_cond_timedwait(&cond->cond, &cond->mutex, abstime); //可以被pthread_cond_signal函数,pthread_cond_broadcast函数唤醒,也可能在被信号中断后被唤醒
}
void condition_signal(condition_t *cond)
{
pthread_cond_signal(&cond->cond);
}
void condition_broadcast(condition_t *cond)
{
pthread_cond_broadcast(&cond->cond);
}
void condition_destroy(condition_t *cond)
{
pthread_mutex_destroy(&cond->mutex);
pthread_cond_destroy(&cond->cond);
}
#include "threadpool.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void threadpool_init(threadpool_t *pool, int threads)
{
condition_init(&pool->ready);
pool->first = NULL;
pool->last = NULL;
pool->counter = 0;
pool->idle = 0;
pool->max_threads = threads;
pool->quit = 0;
}
void *route(void *arg)
{
threadpool_t *pool = (threadpool_t*)arg;
int timeout = 0;
while ( 1 ) {
condition_lock(&pool->ready);
timeout = 0;
pool->idle++;
// 等待任务队列有任务到来或线程销毁通知
while ( pool->first == NULL && pool->quit == 0 ) {
//condition_wait(&pool->ready);
struct timespec ts; //计时结构体
clock_gettime(CLOCK_REALTIME, &ts); //给程序计时,最小单位为纳秒
ts.tv_sec += 2;
int ret = condition_timedwait(&pool->ready, &ts); //成功返回0
if ( ret == ETIMEDOUT ) {
printf("%#X thread timeout!\n", (int)pthread_self());
timeout = 1;
break;
}
}
// 等待到条件,空闲线程数量减少
pool->idle--;
if ( pool->first != NULL ) {
// 从队头取任务进行执行
task_t *t = pool->first;
pool->first = t->next;
// 防止run函数执行的时间太长
condition_unlock(&pool->ready);
t->run(t->arg);
condition_lock(&pool->ready);
free(t);
}
// 等待到线程池销毁通知,并且任务都执行完毕
if ( pool->quit == 1 && pool->first == NULL ) {
pool->counter--;
if ( pool->counter == 0 )
condition_signal(&pool->ready);
// 跳出循环之前,要记得解锁
condition_unlock(&pool->ready);
break;
}
// 超时处理
if ( timeout == 1 && pool->first == NULL ) {
pool->counter--;
condition_unlock(&pool->ready);
break;
}
condition_unlock(&pool->ready);
}
}
void threadpool_add_task(threadpool_t *pool, void *(*run)(void*), void *arg)
{
task_t *newtask = malloc(sizeof(task_t));
newtask->run = run;
newtask->arg = arg;
newtask->next = NULL;
condition_lock(&pool->ready);
if ( pool->first == NULL )
pool->first = newtask;
else
pool->last->next = newtask;
pool->last = newtask;
if ( pool->idle > 0 ) {
condition_signal(&pool->ready);
} else if ( pool->counter < pool->max_threads ) {
pthread_t tid;
pthread_create(&tid, NULL, route, pool); // (指向线程标识符的指针,线程属性, //线程运行函数的起始地址,运行函数的参数 )
pool->counter++;
}
condition_unlock(&pool->ready);
}
void threadpool_destroy(threadpool_t *pool)
{
if ( pool->quit == 1 )
return;
condition_lock(&pool->ready);
pool->quit = 1;
if ( pool->counter > 0 ) {
if ( pool->idle > 0 )
condition_broadcast(&pool->ready);
// 如果发通知时有线程在工作,通知就丢失
// 所以需要最后一个线程销毁时,给这个地方发送通知
while ( pool->counter > 0 )
condition_wait(&pool->ready);
}
condition_unlock(&pool->ready);
condition_destroy(&pool->ready);
}