线程池简介:
线程池,即用来存放“线程”的对象池。线程的生命周期包括:创建,活动和销毁。每个步骤都会占用一定的cpu时间,当线程执行任务的时间短,且执行的次数比较频繁,那么服务器将处于不停的创建线程和销毁线程的状态。当创建和销毁线程占据了线程总周期的总cpu很大一部分额度后,这样的一笔开销是不容忽视的。
线程池的出现正是主要用来处理减少线程自身所带来的上述开销。线程池采用预创建技术,在应用程序启动后,将立即创建一定数量的线程,并将创建的线程放入空闲队列中,这些线程都处于阻塞状态,并不占用cpu时间,但会消耗一定的内存空间。
线程池通常由1个值守线程和n个任务线程组成,n是线程池的最小尺寸,数值根据服务的特点估算。在实际应用中,线程池的尺寸是需要动态调整的,高峰期线程池通过增加线程来尽可能满足任务的需要,空闲期线程池再缩减至最小尺寸。当然线程池的最小尺寸也无需按预置的固定尺寸,通常可以根据一定时期内任务队列的平均大小来获得一个统计量,进行调整。
值守线程的功能是监视任务队列和维护线程池的尺寸,当任务队列中有任务项目时,每次摘除一个任务并将之投放到线程池的空闲线程中去,当线程池中没有空闲线程时,值守线程负责创建新的线程加入到池中。在任务空闲状态,值守线程销毁超过线程池最小尺寸的空闲线程,以释放系统资源。
任务线程是完成具体应用服务的工作线程,未被任务占据的线程称为空闲状态,在此状态下,我们当然不希望它无效地空转,因为那样同样会消耗CPU的时间,解决办法是它阻塞在后台。任务线程的有效运行是在投放了任务之后,被投放任务的线程在任务完成之前不能再被其他任务占据,我们称之为运行状态。在服务进程退出之前,我们希望任务线程都能够自然终结。鉴于以上原因,我们必须定义一些状态量来控制任务线程的有序运行,可以用如下状态量:空闲态、运行态、终结态,并且为每一个任务线程定义一个任务信号,以下我们再讨论下任务线程状态的转移过程。
值守线程创建新的任务线程,将其状态置为空闲态,并加入到线程池中,处于空闲态的任务线程在等待任务信号的过程中阻塞。当值守线程从任务队列中摘取任务项目后,从线程池中攫取一个空闲状态的线程,把任务项目投放到该线程上,置其状态为运行态,并激活任务信号,这样,阻塞的任务线程恢复运行,执行任务。当任务线程完成任务后,重置任务信号,并将自身状态置为空闲态,回归到线程池中。当值守线程收到服务进程的退出宣告后,将池中的空闲线程的状态置为终结态,投放空任务(NULL),并激活任务信号,阻塞的任务线程恢复运行,探测到自身状态为终结态后,执行退出,线程自然终结。
状态转移的示意图如下:
值守线程和任务线程的流程示意图:
线程池技术主要用于对性能要求苛刻的应用,比如服务器要求迅速响应客户请求。另外,线程池技术还适用于接收突发性的大量请求。同时,线程池也具有一定的风险,如果与线程创建和销毁时间相比任务执行时间可以忽略不计,那么就没必要使用线程池。
以下是线程池的一个实现例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <assert.h>
typedef struct worker //运行和等待的任务链表结构
{
void *(*process) (void *arg); //任务运行时会调用该函数
void *arg;
struct worker *next;
} CThread_worker;
typedef struct //线程池结构
{
pthread_mutex_t queue_lock;
pthread_cond_t queue_ready;
CThread_worker *queue_head; //任务链表结构,用于存储线程池中所有的等待任务
int shutdown; //标志符,用于表示是否销毁线程池,1代表销毁,反之0
pthread_t *threadid;
int max_thread_num; //线程池中允许活动的线程数目
int cur_queue_size; //当前任务链表的数目
} CThread_pool;
int pool_add_worker (void *(*process) (void *arg), void *arg); //向线程池中添加一个任务
void *thread_routine (void *arg); //线程体函数
static CThread_pool *pool = NULL;
void
pool_init (int max_thread_num) //预先创建max_thread_num个线程
{
pool = (CThread_pool *) malloc (sizeof (CThread_pool));
pthread_mutex_init (&(pool->queue_lock), NULL);
pthread_cond_init (&(pool->queue_ready), NULL);
//初始化
pool->queue_head = NULL;
pool->max_thread_num = max_thread_num;
pool->cur_queue_size = 0;
pool->shutdown = 0;
pool->threadid = (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));
int i = 0;
for (i = 0; i < max_thread_num; i++) //创建新线程
{
pthread_create (&(pool->threadid[i]), NULL, thread_routine, NULL);
}
}
int
pool_add_worker (void *(*process) (void *arg), void *arg)
{
CThread_worker *newworker =
(CThread_worker *) malloc (sizeof (CThread_worker));
newworker->process = process;
newworker->arg = arg;
newworker->next = NULL;
pthread_mutex_lock (&(pool->queue_lock));
CThread_worker *member = pool->queue_head; //定义一个新任务并且初始化为第一个任务链表结构
//创建一个任务队列
if (member != NULL)
{
while (member->next != NULL)
member = member->next;
member->next = newworker;
}
else
{
pool->queue_head = newworker;
}
assert (pool->queue_head != NULL);
pool->cur_queue_size++;
pthread_mutex_unlock (&(pool->queue_lock));
pthread_cond_signal (&(pool->queue_ready)); //唤醒一个等待线程,如果所有线程都为忙碌,则该语句不起任何作用
return 0;
}
int
pool_destroy () //销毁线程池
{
if (pool->shutdown)
return -1;
pool->shutdown = 1;
pthread_cond_broadcast (&(pool->queue_ready)); //唤醒所有等待线程
int i;
for (i = 0; i < pool->max_thread_num; i++)
{
pthread_join (pool->threadid[i], NULL); //阻塞等待所有线程退出
}
free (pool->threadid);
CThread_worker *head = NULL;
while (pool->queue_head != NULL)
{
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free (head);
}
pthread_mutex_destroy (&(pool->queue_lock));
pthread_cond_destroy (&(pool->queue_ready));
free (pool);
pool = NULL;
return 0;
}
void *
thread_routine (void *arg) //线程体函数
{
printf ("starting thread 0x%x\n", pthread_self ());
while (1)
{
pthread_mutex_lock (&(pool->queue_lock));
while (pool->cur_queue_size == 0 && !pool->shutdown)
{
printf ("thread 0x%x is waiting\n", pthread_self ());
pthread_cond_wait (&(pool->queue_ready), &(pool->queue_lock)); //所有预先创建的线程阻塞等待条件成立
}
if (pool->shutdown) //如果打算销毁线程
{
pthread_mutex_unlock (&(pool->queue_lock));
printf ("thread 0x%x will exit \n", pthread_self ());
pthread_exit (NULL);
}
printf ("thread 0x%x is starting to work\n", pthread_self ());
assert (pool->cur_queue_size != 0);
assert (pool->queue_head != NULL);
pool->cur_queue_size--;
CThread_worker *worker = pool->queue_head; //取出第一个任务
pool->queue_head = worker->next;
pthread_mutex_unlock (&(pool->queue_lock));
(*(worker->process)) (worker->arg); //执行该任务
free (worker);
worker = NULL;
}
pthread_exit (NULL);
}
//以下是测试代码
void *myprocess (void *arg)
{
printf ("thread is 0x%x, working on task %d\n", pthread_self (), *(int *) arg);
sleep (1);
return NULL;
}
int main (int ac, char *av[])
{
pool_init (3); //预先创建3个活动线程
//投入十个任务
int *workingnum = (int *) malloc (sizeof (int ) * 10);
int i;
for (i = 0; i < 10; i++)
{
workingnum[i] = i;
pool_add_worker (myprocess, &workingnum[i]);
}
sleep (5);
pool_destroy ();
free (workingnum);
return 0;
}
下面是测试结果: