简单Linux C线程池
大多數(shù)的網(wǎng)絡(luò)服務(wù)器,包括Web服務(wù)器都具有一個(gè)特點(diǎn),就是單位時(shí)間內(nèi)必須處理數(shù)目巨大的連接請(qǐng)求,但是處理時(shí)間卻是比較短的。在傳統(tǒng)的多線程服務(wù)器模型中是這樣實(shí)現(xiàn)的:一旦有個(gè)請(qǐng)求到達(dá),就創(chuàng)建一個(gè)新的線程,由該線程執(zhí)行任務(wù),任務(wù)執(zhí)行完畢之后,線程就退出。這就是"即時(shí)創(chuàng)建,即時(shí)銷(xiāo)毀"的策略。盡管與創(chuàng)建進(jìn)程相比,創(chuàng)建線程的時(shí)間已經(jīng)大大的縮短,但是如果提交給線程的任務(wù)是執(zhí)行時(shí)間較短,而且執(zhí)行次數(shù)非常頻繁,那么服務(wù)器就將處于一個(gè)不停的創(chuàng)建線程和銷(xiāo)毀線程的狀態(tài)。這筆開(kāi)銷(xiāo)是不可忽略的,尤其是線程執(zhí)行的時(shí)間非常非常短的情況。
線程池就是為了解決上述問(wèn)題的,它的實(shí)現(xiàn)原理是這樣的:在應(yīng)用程序啟動(dòng)之后,就馬上創(chuàng)建一定數(shù)量的線程,放入空閑的隊(duì)列中。這些線程都是處于阻塞狀態(tài),這些線程只占一點(diǎn)內(nèi)存,不占用CPU。當(dāng)任務(wù)到來(lái)后,線程池將選擇一個(gè)空閑的線程,將任務(wù)傳入此線程中運(yùn)行。當(dāng)所有的線程都處在處理任務(wù)的時(shí)候,線程池將自動(dòng)創(chuàng)建一定的數(shù)量的新線程,用于處理更多的任務(wù)。執(zhí)行任務(wù)完成之后線程并不退出,而是繼續(xù)在線程池中等待下一次任務(wù)。當(dāng)大部分線程處于阻塞狀態(tài)時(shí),線程池將自動(dòng)銷(xiāo)毀一部分的線程,回收系統(tǒng)資源。
下面是一個(gè)簡(jiǎn)單線程池的實(shí)現(xiàn),這個(gè)線程池的代碼是我參考網(wǎng)上的一個(gè)例子實(shí)現(xiàn)的,由于找不到出處了,就沒(méi)辦法注明參考自哪里了。它的方案是這樣的:程序啟動(dòng)之前,初始化線程池,啟動(dòng)線程池中的線程,由于還沒(méi)有任務(wù)到來(lái),線程池中的所有線程都處在阻塞狀態(tài),當(dāng)一有任務(wù)到達(dá)就從線程池中取出一個(gè)空閑線程處理,如果所有的線程都處于工作狀態(tài),就添加到隊(duì)列,進(jìn)行排隊(duì)。如果隊(duì)列中的任務(wù)個(gè)數(shù)大于隊(duì)列的所能容納的最大數(shù)量,那就不能添加任務(wù)到隊(duì)列中,只能等待隊(duì)列不滿(mǎn)才能添加任務(wù)到隊(duì)列中。
主要由兩個(gè)文件組成一個(gè)threadpool.h頭文件和一個(gè)threadpool.c源文件組成。源碼中已有重要的注釋,就不加以分析了。
threadpool.h文件:
struct job {void* (*callback_function)(void *arg); //線程回調(diào)函數(shù)void *arg; //回調(diào)函數(shù)參數(shù)struct job *next; };struct threadpool {int thread_num; //線程池中開(kāi)啟線程的個(gè)數(shù)int queue_max_num; //隊(duì)列中最大job的個(gè)數(shù)struct job *head; //指向job的頭指針struct job *tail; //指向job的尾指針pthread_t *pthreads; //線程池中所有線程的pthread_tpthread_mutex_t mutex; //互斥信號(hào)量pthread_cond_t queue_empty; //隊(duì)列為空的條件變量pthread_cond_t queue_not_empty; //隊(duì)列不為空的條件變量pthread_cond_t queue_not_full; //隊(duì)列不為滿(mǎn)的條件變量int queue_cur_num; //隊(duì)列當(dāng)前的job個(gè)數(shù)int queue_close; //隊(duì)列是否已經(jīng)關(guān)閉int pool_close; //線程池是否已經(jīng)關(guān)閉 };//================================================================================================ //函數(shù)名: threadpool_init //函數(shù)描述: 初始化線程池 //輸入: [in] thread_num 線程池開(kāi)啟的線程個(gè)數(shù) // [in] queue_max_num 隊(duì)列的最大job個(gè)數(shù) //輸出: 無(wú) //返回: 成功:線程池地址 失敗:NULL //================================================================================================ struct threadpool* threadpool_init(int thread_num, int queue_max_num);//================================================================================================ //函數(shù)名: threadpool_add_job //函數(shù)描述: 向線程池中添加任務(wù) //輸入: [in] pool 線程池地址 // [in] callback_function 回調(diào)函數(shù) // [in] arg 回調(diào)函數(shù)參數(shù) //輸出: 無(wú) //返回: 成功:0 失敗:-1 //================================================================================================ int threadpool_add_job(struct threadpool *pool, void* (*callback_function)(void *arg), void *arg);//================================================================================================ //函數(shù)名: threadpool_destroy //函數(shù)描述: 銷(xiāo)毀線程池 //輸入: [in] pool 線程池地址 //輸出: 無(wú) //返回: 成功:0 失敗:-1 //================================================================================================ int threadpool_destroy(struct threadpool *pool);//================================================================================================ //函數(shù)名: threadpool_function //函數(shù)描述: 線程池中線程函數(shù) //輸入: [in] arg 線程池地址 //輸出: 無(wú) //返回: 無(wú) //================================================================================================ 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)); //隊(duì)列滿(mǎn)的時(shí)候就等待 }if (pool->queue_close || pool->pool_close) //隊(duì)列關(guān)閉或者線程池關(guān)閉就退出 {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)); //隊(duì)列空的時(shí)候,有任務(wù)來(lái)時(shí)就通知線程池中的線程:隊(duì)列非空 }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) //死循環(huán) {pthread_mutex_lock(&(pool->mutex));while ((pool->queue_cur_num == 0) && !pool->pool_close) //隊(duì)列為空時(shí),就等待隊(duì)列非空 {pthread_cond_wait(&(pool->queue_not_empty), &(pool->mutex));}if (pool->pool_close) //線程池關(guān)閉,線程就退出 {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)); //隊(duì)列為空,就可以通知threadpool_destroy函數(shù),銷(xiāo)毀線程函數(shù) }if (pool->queue_cur_num == pool->queue_max_num - 1){pthread_cond_broadcast(&(pool->queue_not_full)); //隊(duì)列非滿(mǎn),就可以通知threadpool_add_job函數(shù),添加新任務(wù) }pthread_mutex_unlock(&(pool->mutex));(*(pjob->callback_function))(pjob->arg); //線程真正要做的工作,回調(diào)函數(shù)的調(diào)用 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) //線程池已經(jīng)退出了,就直接返回 {pthread_mutex_unlock(&(pool->mutex));return -1;}pool->queue_close = 1; //置隊(duì)列關(guān)閉標(biāo)志while (pool->queue_cur_num != 0){pthread_cond_wait(&(pool->queue_empty), &(pool->mutex)); //等待隊(duì)列為空 } pool->pool_close = 1; //置線程池關(guān)閉標(biāo)志pthread_mutex_unlock(&(pool->mutex));pthread_cond_broadcast(&(pool->queue_not_empty)); //喚醒線程池中正在阻塞的線程pthread_cond_broadcast(&(pool->queue_not_full)); //喚醒添加任務(wù)的threadpool_add_job函數(shù)int i;for (i = 0; i < pool->thread_num; ++i){pthread_join(pool->pthreads[i], NULL); //等待線程池的所有線程執(zhí)行完畢 }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; }測(cè)試文件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編譯,運(yùn)行就可以看到效果,1到40個(gè)回調(diào)函數(shù)分別被執(zhí)行。
總結(jié)
以上是生活随笔為你收集整理的简单Linux C线程池的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Lync 2010迁移Lync 2013
- 下一篇: linux alias命令参数及用法详解