資源簡介
一個linux下的c實(shí)現(xiàn)的線程池,其中包括線程池的創(chuàng)建、銷毀、線程狀態(tài)等操作。

代碼片段和文件信息
#include?“thread-pool.h“
static?void?*tp_work_thread(void?*pthread);
static?void?*tp_manage_thread(void?*pthread);
static?TPBOOL?tp_init(tp_thread_pool?*this);
static?void?tp_close(tp_thread_pool?*this);
static?void?tp_process_job(tp_thread_pool?*this?tp_work?*worker?tp_work_desc?*job);
static?int??tp_get_thread_by_id(tp_thread_pool?*this?int?id);
static?TPBOOL?tp_add_thread(tp_thread_pool?*this);
static?TPBOOL?tp_delete_thread(tp_thread_pool?*this);
static?int??tp_get_tp_status(tp_thread_pool?*this);
/**
??*?user?interface.?creat?thread?pool.
??*?para:
??*? num:?min?thread?number?to?be?created?in?the?pool
??*?return:
??*? thread?pool?struct?instance?be?created?successfully
??*/
tp_thread_pool?*creat_thread_pool(int?min_num?int?max_num){
tp_thread_pool?*this;
this?=?(tp_thread_pool*)malloc(sizeof(tp_thread_pool));
memset(this?0?sizeof(tp_thread_pool));
//init?member?function?ponter
this->init?=?tp_init;
this->close?=?tp_close;
this->process_job?=?tp_process_job;
this->get_thread_by_id?=?tp_get_thread_by_id;
this->add_thread?=?tp_add_thread;
this->delete_thread?=?tp_delete_thread;
this->get_tp_status?=?tp_get_tp_status;
//init?member?var
this->min_th_num?=?min_num;
this->cur_th_num?=?this->min_th_num;
this->max_th_num?=?max_num;
pthread_mutex_init(&this->tp_lock?NULL);
//malloc?mem?for?num?thread?info?struct
if(NULL?!=?this->thread_info)
free(this->thread_info);
this->thread_info?=?(tp_thread_info*)malloc(sizeof(tp_thread_info)*this->max_th_num);
return?this;
}
/**
??*?member?function?reality.?thread?pool?init?function.
??*?para:
??*? this:?thread?pool?struct?instance?ponter
??*?return:
??*? true:?successful;?false:?failed
??*/
TPBOOL?tp_init(tp_thread_pool?*this){
int?i;
int?err;
//creat?work?thread?and?init?work?thread?info
for(i=0;imin_th_num;i++){
pthread_cond_init(&this->thread_info[i].thread_cond?NULL);
pthread_mutex_init(&this->thread_info[i].thread_lock?NULL);
err?=?pthread_create(&this->thread_info[i].thread_id?NULL?tp_work_thread?this);
if(0?!=?err){
printf(“tp_init:?creat?work?thread?failed\n“);
return?FALSE;
}
printf(“tp_init:?creat?work?thread?%d\n“?this->thread_info[i].thread_id);
}
//creat?manage?thread
err?=?pthread_create(&this->manage_thread_id?NULL?tp_manage_thread?this);
if(0?!=?err){
printf(“tp_init:?creat?manage?thread?failed\n“);
return?FALSE;
}
printf(“tp_init:?creat?manage?thread?%d\n“?this->manage_thread_id);
return?TRUE;
}
/**
??*?member?function?reality.?thread?pool?entirely?close?function.
??*?para:
??*? this:?thread?pool?struct?instance?ponter
??*?return:
??*/
void?tp_close(tp_thread_pool?*this){
int?i;
//close?work?thread
for(i=0;icur_th_num;i++){
kill(this->thread_info[i].thread_id?SIGKILL);
pthread_mutex_destroy(&this->thread_info[i].thread_lock);
pthread_cond_destroy(&this->thread_info[i].thread_cond);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????74??2007-08-17?14:28??libthreadpool\AUTHORS
?????文件??????18009??2007-08-17?14:28??libthreadpool\COPYING
?????目錄??????????0??2007-08-17?14:28??libthreadpool\example
?????文件????????168??2007-08-17?14:28??libthreadpool\INSTALL
?????文件??????37888??2007-08-17?14:28??libthreadpool\linux線程池的C語言實(shí)現(xiàn).doc
?????文件????????640??2007-08-17?14:28??libthreadpool\Makefile
?????文件????????107??2007-08-17?14:28??libthreadpool\README
?????文件???????9740??2007-08-17?14:28??libthreadpool\src\thread-pool.c
?????文件???????1873??2007-08-17?14:28??libthreadpool\src\thread-pool.h
?????目錄??????????0??2007-08-17?14:28??libthreadpool\src
?????目錄??????????0??2007-08-17?14:28??libthreadpool
-----------?---------??----------?-----??----
????????????????68717????????????????????12
評論
共有 條評論