本文轉載自:http://www.cnblogs.com/venow/archive/2012/11/22/2779667.html
大多數的網絡服務器,包括Web服務器都具有一個特點,就是單位時間內必須處理數目巨大的連接請求,但是處理時間卻是比較短的。在傳統的多線程服務器模型中是這樣實現的:一旦有個請求到達,就創建一個新的線程,由該線程執行任務,任務執行完畢之後,線程就退出。這就是"即時創建,即時銷燬"的策略。儘管與創建進程相比,創建線程的時間已經大大的縮短,但是如果提交給線程的任務是執行時間較短,而且執行次數非常頻繁,那麼服務器就將處於一個不停的創建線程和銷燬線程的狀態。這筆開銷是不可忽略的,尤其是線程執行的時間非常非常短的情況。
線程池就是爲了解決上述問題的,它的實現原理是這樣的:在應用程序啓動之後,就馬上創建一定數量的線程,放入空閒的隊列中。這些線程都是處於阻塞狀態,這些線程只佔一點內存,不佔用CPU。當任務到來後,線程池將選擇一個空閒的線程,將任務傳入此線程中運行。當所有的線程都處在處理任務的時候,線程池將自動創建一定的數量的新線程,用於處理更多的任務。執行任務完成之後線程並不退出,而是繼續在線程池中等待下一次任務。當大部分線程處於阻塞狀態時,線程池將自動銷燬一部分的線程,回收系統資源。
下面是一個簡單線程池的實現,這個線程池的代碼是我參考網上的一個例子實現的,由於找不到出處了,就沒辦法註明參考自哪裏了。它的方案是這樣的:程序啓動之前,初始化線程池,啓動線程池中的線程,由於還沒有任務到來,線程池中的所有線程都處在阻塞狀態,當一有任務到達就從線程池中取出一個空閒線程處理,如果所有的線程都處於工作狀態,就添加到隊列,進行排隊。如果隊列中的任務個數大於隊列的所能容納的最大數量,那就不能添加任務到隊列中,只能等待隊列不滿才能添加任務到隊列中。
主要由兩個文件組成一個threadpool.h頭文件和一個threadpool.c源文件組成。源碼中已有重要的註釋,就不加以分析了。
threadpool.h文件:
struct job
{
void* (*callback_function)(void *arg); //線程回調函數
void *arg; //回調函數參數
struct job *next;
};
struct threadpool
{
int thread_num; //線程池中開啓線程的個數
int queue_max_num; //隊列中最大job的個數
struct job *head; //指向job的頭指針
struct job *tail; //指向job的尾指針
pthread_t *pthreads; //線程池中所有線程的pthread_t
pthread_mutex_t mutex; //互斥信號量
pthread_cond_t queue_empty; //隊列爲空的條件變量
pthread_cond_t queue_not_empty; //隊列不爲空的條件變量
pthread_cond_t queue_not_full; //隊列不爲滿的條件變量
int queue_cur_num; //隊列當前的job個數
int queue_close; //隊列是否已經關閉
int pool_close; //線程池是否已經關閉
};
//================================================================================================
//函數名: threadpool_init
//函數描述: 初始化線程池
//輸入: [in] thread_num 線程池開啓的線程個數
// [in] queue_max_num 隊列的最大job個數
//輸出: 無
//返回: 成功:線程池地址 失敗:NULL
//================================================================================================
struct threadpool* threadpool_init(int thread_num, int queue_max_num);
//================================================================================================
//函數名: threadpool_add_job
//函數描述: 向線程池中添加任務
//輸入: [in] pool 線程池地址
// [in] callback_function 回調函數
// [in] arg 回調函數參數
//輸出: 無
//返回: 成功:0 失敗:-1
//================================================================================================
int threadpool_add_job(struct threadpool *pool, void* (*callback_function)(void *arg), void *arg);
//================================================================================================
//函數名: threadpool_destroy
//函數描述: 銷燬線程池
//輸入: [in] pool 線程池地址
//輸出: 無
//返回: 成功:0 失敗:-1
//================================================================================================
int threadpool_destroy(struct threadpool *pool);
//================================================================================================
//函數名: threadpool_function
//函數描述: 線程池中線程函數
//輸入: [in] arg 線程池地址
//輸出: 無
//返回: 無
//================================================================================================
void* threadpool_function(void* arg);
threadpool.c文件:
#include "threadpool.h"
struct threadpool* threadpool_init(int thread_num, int queue_max_num)
{
struct threadpool *pool = NULL;
do
{
pool = malloc(sizeof(struct threadpool));
if (NULL == pool)
{
printf("failed to malloc threadpool!\n");
break;
}
pool->thread_num = thread_num;
pool->queue_max_num = queue_max_num;
pool->queue_cur_num = 0;
pool->head = NULL;
pool->tail = NULL;
if (pthread_mutex_init(&(pool->mutex), NULL))
{
printf("failed to init mutex!\n");
break;
}
if (pthread_cond_init(&(pool->queue_empty), NULL))
{
printf("failed to init queue_empty!\n");
break;
}
if (pthread_cond_init(&(pool->queue_not_empty), NULL))
{
printf("failed to init queue_not_empty!\n");
break;
}
if (pthread_cond_init(&(pool->queue_not_full), NULL))
{
printf("failed to init queue_not_full!\n");
break;
}
pool->pthreads = malloc(sizeof(pthread_t) * thread_num);
if (NULL == pool->pthreads)
{
printf("failed to malloc pthreads!\n");
break;
}
pool->queue_close = 0;
pool->pool_close = 0;
int i;
for (i = 0; i < pool->thread_num; ++i)
{
pthread_create(&(pool->pthreads[i]), NULL, threadpool_function, (void *)pool);
}
return pool;
} while (0);
return NULL;
}
int threadpool_add_job(struct threadpool* pool, void* (*callback_function)(void *arg), void *arg)
{
assert(pool != NULL);
assert(callback_function != NULL);
assert(arg != NULL);
pthread_mutex_lock(&(pool->mutex));
while ((pool->queue_cur_num == pool->queue_max_num) && !(pool->queue_close || pool->pool_close))
{
pthread_cond_wait(&(pool->queue_not_full), &(pool->mutex)); //隊列滿的時候就等待
}
if (pool->queue_close || pool->pool_close) //隊列關閉或者線程池關閉就退出
{
pthread_mutex_unlock(&(pool->mutex));
return -1;
}
struct job *pjob =(struct job*) malloc(sizeof(struct job));
if (NULL == pjob)
{
pthread_mutex_unlock(&(pool->mutex));
return -1;
}
pjob->callback_function = callback_function;
pjob->arg = arg;
pjob->next = NULL;
if (pool->head == NULL)
{
pool->head = pool->tail = pjob;
pthread_cond_broadcast(&(pool->queue_not_empty)); //隊列空的時候,有任務來時就通知線程池中的線程:隊列非空
}
else
{
pool->tail->next = pjob;
pool->tail = pjob;
}
pool->queue_cur_num++;
pthread_mutex_unlock(&(pool->mutex));
return 0;
}
void* threadpool_function(void* arg)
{
struct threadpool *pool = (struct threadpool*)arg;
struct job *pjob = NULL;
while (1) //死循環
{
pthread_mutex_lock(&(pool->mutex));
while ((pool->queue_cur_num == 0) && !pool->pool_close) //隊列爲空時,就等待隊列非空
{
pthread_cond_wait(&(pool->queue_not_empty), &(pool->mutex));
}
if (pool->pool_close) //線程池關閉,線程就退出
{
pthread_mutex_unlock(&(pool->mutex));
pthread_exit(NULL);
}
pool->queue_cur_num--;
pjob = pool->head;
if (pool->queue_cur_num == 0)
{
pool->head = pool->tail = NULL;
}
else
{
pool->head = pjob->next;
}
if (pool->queue_cur_num == 0)
{
pthread_cond_signal(&(pool->queue_empty)); //隊列爲空,就可以通知threadpool_destroy函數,銷燬線程函數
}
if (pool->queue_cur_num == pool->queue_max_num - 1)
{
pthread_cond_broadcast(&(pool->queue_not_full)); //隊列非滿,就可以通知threadpool_add_job函數,添加新任務
}
pthread_mutex_unlock(&(pool->mutex));
(*(pjob->callback_function))(pjob->arg); //線程真正要做的工作,回調函數的調用
free(pjob);
pjob = NULL;
}
}
int threadpool_destroy(struct threadpool *pool)
{
assert(pool != NULL);
pthread_mutex_lock(&(pool->mutex));
if (pool->queue_close || pool->pool_close) //線程池已經退出了,就直接返回
{
pthread_mutex_unlock(&(pool->mutex));
return -1;
}
pool->queue_close = 1; //置隊列關閉標誌
while (pool->queue_cur_num != 0)
{
pthread_cond_wait(&(pool->queue_empty), &(pool->mutex)); //等待隊列爲空
}
pool->pool_close = 1; //置線程池關閉標誌
pthread_mutex_unlock(&(pool->mutex));
pthread_cond_broadcast(&(pool->queue_not_empty)); //喚醒線程池中正在阻塞的線程
pthread_cond_broadcast(&(pool->queue_not_full)); //喚醒添加任務的threadpool_add_job函數
int i;
for (i = 0; i < pool->thread_num; ++i)
{
pthread_join(pool->pthreads[i], NULL); //等待線程池的所有線程執行完畢
}
pthread_mutex_destroy(&(pool->mutex)); //清理資源
pthread_cond_destroy(&(pool->queue_empty));
pthread_cond_destroy(&(pool->queue_not_empty));
pthread_cond_destroy(&(pool->queue_not_full));
free(pool->pthreads);
struct job *p;
while (pool->head != NULL)
{
p = pool->head;
pool->head = p->next;
free(p);
}
free(pool);
return 0;
}
測試文件main.c文件:
#include "threadpool.h"
void* work(void* arg)
{
char *p = (char*) arg;
printf("threadpool callback fuction : %s.\n", p);
sleep(1);
}
int main(void)
{
struct threadpool *pool = threadpool_init(10, 20);
threadpool_add_job(pool, work, "1");
threadpool_add_job(pool, work, "2");
threadpool_add_job(pool, work, "3");
threadpool_add_job(pool, work, "4");
threadpool_add_job(pool, work, "5");
threadpool_add_job(pool, work, "6");
threadpool_add_job(pool, work, "7");
threadpool_add_job(pool, work, "8");
threadpool_add_job(pool, work, "9");
threadpool_add_job(pool, work, "10");
threadpool_add_job(pool, work, "11");
threadpool_add_job(pool, work, "12");
threadpool_add_job(pool, work, "13");
threadpool_add_job(pool, work, "14");
threadpool_add_job(pool, work, "15");
threadpool_add_job(pool, work, "16");
threadpool_add_job(pool, work, "17");
threadpool_add_job(pool, work, "18");
threadpool_add_job(pool, work, "19");
threadpool_add_job(pool, work, "20");
threadpool_add_job(pool, work, "21");
threadpool_add_job(pool, work, "22");
threadpool_add_job(pool, work, "23");
threadpool_add_job(pool, work, "24");
threadpool_add_job(pool, work, "25");
threadpool_add_job(pool, work, "26");
threadpool_add_job(pool, work, "27");
threadpool_add_job(pool, work, "28");
threadpool_add_job(pool, work, "29");
threadpool_add_job(pool, work, "30");
threadpool_add_job(pool, work, "31");
threadpool_add_job(pool, work, "32");
threadpool_add_job(pool, work, "33");
threadpool_add_job(pool, work, "34");
threadpool_add_job(pool, work, "35");
threadpool_add_job(pool, work, "36");
threadpool_add_job(pool, work, "37");
threadpool_add_job(pool, work, "38");
threadpool_add_job(pool, work, "39");
threadpool_add_job(pool, work, "40");
sleep(5);
threadpool_destroy(pool);
return 0;
}
用gcc編譯,運行就可以看到效果,1到40個回調函數分別被執行。