ʲôÊÇÏ̳߳أ¿
¼òµ¥µã˵£¬Ï̳߳ؾÍÊÇÓÐÒ»¶ÑÒѾ´´½¨ºÃÁ˵ÄỊ̈߳¬³õʼËüÃǶ¼´¦ÓÚ¿ÕÏеȴý״̬£¬µ±ÓÐеÄÈÎÎñÐèÒª´¦ÀíµÄʱºò£¬¾Í´ÓÕâ¸ö³Ø×ÓÀïÃæÈ¡Ò»¸ö¿ÕÏÐµÈ ´ýµÄÏß³ÌÀ´´¦Àí¸ÃÈÎÎñ£¬µ±´¦ÀíÍê³ÉÁ˾ÍÔٴΰѸÃÏ̷߳ŻسØÖУ¬ÒÔ¹©ºóÃæµÄÈÎÎñʹÓᣵ±³Ø×ÓÀïµÄÏß³ÌÈ«¶¼´¦Àíæµ״̬ʱ£¬Ï̳߳ØÖÐûÓпÉÓõĿÕÏеȴýỊ̈߳¬ ´Ëʱ£¬¸ù¾ÝÐèҪѡÔñ´´½¨Ò»¸öеÄÏ̲߳¢ÖÃÈë³ØÖУ¬»òÕß֪ͨÈÎÎñÏ̳߳Ø棬ÉÔºóÔÙÊÔ¡£
ΪʲôҪÓÃÏ̳߳أ¿
ΪʲôҪÓÃÏ̳߳أ¿
ÎÒÃÇ˵£¬Ï̵߳Ĵ´½¨ºÍÏú»Ù±ÈÖ®½ø³ÌµÄ´´½¨ºÍÏú»ÙÊÇÇáÁ¿¼¶µÄ£¬µ«Êǵ±ÎÒÃǵÄÈÎÎñÐèÒª´óÁ¿½øÐдóÁ¿Ï̵߳Ĵ´½¨ºÍÏú»Ù²Ù×÷ʱ£¬Õâ¸öÏûºÄ¾Í»á±ä³ÉµÄÏ൱´ó¡£±ÈÈ磬 µ±ÄãÉè¼ÆÒ»¸öѹÁ¦ÐÔÄܲâÊÔ¿ò¼ÜµÄʱºò£¬ÐèÒªÁ¬Ðø²úÉú´óÁ¿µÄ²¢·¢²Ù×÷£¬Õâ¸öÊÇʱºò£¬Ï̳߳ؾͿÉÒԺܺõİïÉÏÄãµÄæ¡£Ï̳߳صĺô¦¾ÍÔÚÓÚÏ̸߳´Óã¬Ò»¸öÈÎÎñ´¦ÀíÍê³Éºó£¬µ±Ç°Ï߳̿ÉÒÔÖ±½Ó´¦ÀíÏÂÒ»¸öÈÎÎñ£¬¶ø²»ÊÇÏú»ÙºóÔÙ´´½¨£¬·Ç³£ÊÊÓÃÓÚÁ¬Ðø²úÉú´óÁ¿²¢·¢ÈÎÎñµÄ³¡ºÏ¡£
Ï̳߳ع¤×÷ÔÀí
Ï̳߳ØÖÐÿһ¸öÏ̵߳Ť×÷¹ý³ÌÈçÏ£º
ͼ 1£º Ï̵߳Ť×÷Á÷³Ì
Ï̳߳صÄÈÎÎñ¾ÍÔÚÓÚ¸ºÔðÕâЩÏ̵߳Ĵ´½¨£¬Ïú»ÙºÍÈÎÎñ´¦Àí²ÎÊý
´«µÝ¡¢»½ÐѺ͵ȴý¡£
1. ´´½¨Èô¸ÉỊ̈߳¬ÖÃÈëÏ̳߳Ø
2. ÈÎÎñ´ïµ½Ê±£¬´ÓÏ̳߳ØÈ¡¿ÕÏÐÏß³Ì
3. È¡µÃÁË¿ÕÏÐỊ̈߳¬Á¢¼´½øÐÐÈÎÎñ´¦Àí
4. ·ñÔòн¨Ò»¸öỊ̈߳¬²¢ÖÃÈëÏ̳߳أ¬Ö´ÐÐ3
5. Èç¹û´´½¨Ê§°Ü»òÕßÏ̳߳ØÒÑÂú£¬¸ù¾ÝÉè¼Æ²ßÂÔÑ¡Ôñ·µ»Ø´íÎó»ò½«ÈÎÎñÖÃÈë´¦Àí¶ÓÁУ¬µÈ´ý´¦Àí
6. Ïú»ÙÏ̳߳Ø
Ï̳߳ØÉè¼Æ
Êý¾Ý½á¹¹Éè¼Æ
ÈÎÎñÉè¼Æ
[cpp] view plaincopy
typedef struct tp_work_desc_s TpWorkDesc;
typedef void (*process_job)(TpWorkDesc*job);
struct tp_work_desc_s {
void *ret; //call in, that is arguments
void *arg; //call out, that is return value
};
ÆäÖУ¬TpWorkDescÊÇÈÎÎñ²ÎÊýÃèÊö£¬argÊÇ´«µÝ¸øÈÎÎñµÄ²ÎÊý£¬retÔòÊÇÈÎÎñ´¦ÀíÍê³ÉºóµÄ·µ»ØÖµ£»
process_jobº¯ÊýÊÇÈÎÎñ´¦Àíº¯ÊýÔÐÍ£¬Ã¿¸öÈÎÎñ´¦Àíº¯Êý¶¼Ó¦¸ÃÕâÑù¶¨Ò壬Ȼºó½«Ëü×÷Ϊ²ÎÊý´«¸øÏ̳߳ش¦Àí£¬Ï̳߳ؽ«»áÑ¡ÔñÒ»¸ö¿ÕÏÐÏß³Ìͨ¹ýµ÷Óøú¯ÊýÀ´½øÐÐÈÎÎñ´¦Àí£»
Ïß³ÌÉè¼Æ
[cpp] view plaincopy
typedef struct tp_thread_info_s TpThreadInfo;
struct tp_thread_info_s {
pthread_t thread_id; //thread id num
TPBOOL is_busy; //thread status:true-busy;flase-idle
pthread_cond_t thread_cond;
pthread_mutex_t thread_lock;
process_job proc_fun;
TpWorkDesc* th_job;
TpThreadPool* tp_pool;
};
TpThreadInfoÊǶÔÒ»¸öÏ̵߳ÄÃèÊö¡£
thread_idÊǸÃÏ̵߳ÄID£»
is_busyÓÃÓÚ±êʶ¸ÃÏß³ÌÊÇ·ñÕý´¦Àíæµ״̬£»
thread_condÓÃÓÚÈÎÎñ´¦ÀíʱµÄ»½ÐѺ͵ȴý£»
thread_lock£¬ÓÃÓÚÈÎÎñ¼ÓËø£¬ÓÃÓÚÌõ¼þ±äÁ¿µÈ´ý¼ÓËø£»
proc_funÊǵ±Ç°ÈÎÎñµÄ»Øµ÷º¯ÊýµØÖ·£»
th_jobÊÇÈÎÎñµÄ²ÎÊýÐÅÏ¢£»
tp_poolÊÇËùÔÚÏ̳߳صÄÖ¸Õ룻
Ï̳߳ØÉè¼Æ
[cpp] view plaincopy
typedef struct tp_thread_pool_s TpThreadPool;
struct tp_thread_pool_s {
unsigned min_th_num; //min thread number in the pool
unsigned cur_th_num; //current thread number in the pool
unsigned max_th_num; //max thread number in the pool
pthread_mutex_t tp_lock;
pthread_t manage_thread_id; //manage thread id num
TpThreadInfo* thread_info;
Queue idle_q;
TPBOOL stop_flag;
};
TpThreadPoolÊǶÔÏ̳߳صÄÃèÊö¡£
min_th_numÊÇÏ̳߳ØÖÐÖÁÉÙ´æÔÚµÄÏß³ÌÊý£¬Ï̳߳سõʼ»¯µÄ¹ý³ÌÖлᴴ½¨min_th_numÊýÁ¿µÄỊ̈߳»
cur_th_numÊÇÏ̳߳ص±Ç°´æÔÚµÄÏß³ÌÊýÁ¿£»
max_th_numÔòÊÇÏ̳߳Ø×î¶à¿ÉÒÔ´æÔÚµÄÏß³ÌÊýÁ¿£»
tp_lockÓÃÓÚÏ̳߳عÜÀíʱµÄ»¥³â£»
manage_thread_idÊÇÏ̳߳صĹÜÀíÏß³ÌID£»
thread_infoÔòÊÇÖ¸ÏòÏ̳߳ØÊý¾Ý£¬ÕâÀïʹÓÃÒ»¸öÊý×éÀ´´æ´¢Ï̳߳ØÖÐÏ̵߳ÄÐÅÏ¢£¬¸ÃÊý×éµÄ´óСΪmax_th_num£»
idle_qÊÇ´æ´¢Ï̳߳ؿÕÏÐÏß³ÌÖ¸ÕëµÄ¶ÓÁУ¬ÓÃÓÚ´ÓÏ̳߳ؿìËÙÈ¡µÃ¿ÕÏÐỊ̈߳»
stop_flagÓÃÓÚÏ̳߳صÄÏú»Ù£¬µ±stop_flagΪFALSEʱ£¬±íÃ÷µ±Ç°Ï̳߳ØÐèÒªÏú»Ù£¬ËùÓÐæµÏß³ÌÔÚ´¦ÀíÍ굱ǰÈÎÎñºó»áÍ˳ö£»
Ëã·¨Éè¼Æ
Ï̳߳صĴ´½¨ºÍ³õʼ»¯
Ï̴߳´½¨
´´½¨ÒÁʼ£¬Ï̳߳ØÏß³ÌÈÝÁ¿´óСÉÏÏÞΪmax_th_num£¬³õʼÈÝÁ¿Îªmin_th_num£»
[cpp] view plaincopy
TpThreadPool *tp_create(unsigned min_num, unsigned max_num) {
TpThreadPool *pTp;
pTp = (TpThreadPool*) malloc(sizeof(TpThreadPool));
memset(pTp, 0, sizeof(TpThreadPool));
//init member var
pTp->min_th_num = min_num;
pTp->cur_th_num = min_num;
pTp->max_th_num = max_num;
pthread_mutex_init(&pTp->tp_lock, NULL);
//malloc mem for num thread info struct
if (NULL != pTp->thread_info)
free(pTp->thread_info);
pTp->thread_info = (TpThreadInfo*) malloc(sizeof(TpThreadInfo) * pTp->max_th_num);
memset(pTp->thread_info, 0, sizeof(TpThreadInfo) * pTp->max_th_num);
return pTp;
}
Ï̳߳õʼ»¯
[cpp] view plaincopy
TPBOOL tp_init(TpThreadPool *pTp) {
int i;
int err;
TpThreadInfo *pThi;
initQueue(&pTp->idle_q);
pTp->stop_flag = FALSE;
//create work thread and init work thread info
for (i = 0; i < pTp->min_th_num; i++) {
pThi = pTp->thread_info +i;
pThi->tp_pool = pTp;
pThi->is_busy = FALSE;
pthread_cond_init(&pThi->thread_cond, NULL);
pthread_mutex_init(&pThi->thread_lock, NULL);
pThi->proc_fun = def_proc_fun;
pThi->th_job = NULL;
enQueue(&pTp->idle_q, pThi);
err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, pThi);
if (0 != err) {
perror("tp_init: create work thread failed.");
clearQueue(&pTp->idle_q);
return FALSE;
}
}
//create manage thread
err = pthread_create(&pTp->manage_thread_id, NULL, tp_manage_thread, pTp);
if (0 != err) {
clearQueue(&pTp->idle_q);
printf("tp_init: creat manage thread failed\n");
return FALSE;
}
return TRUE;
}
³õʼÏ̳߳ØÖÐÏß³ÌÊýÁ¿Îªmin_th_num£¬¶ÔÕâЩÏß³ÌÒ»Ò»½øÐгõʼ»¯£»
½«ÕâЩ³õʼ»¯µÄ¿ÕÏÐÏß³ÌÒ»Ò»ÖÃÈë¿ÕÏжÓÁУ»
´´½¨¹ÜÀíỊ̈߳¬ÓÃÓÚ¼à¿ØÏ̳߳صÄ״̬£¬²¢Êʵ±»ØÊÕ¶àÓàµÄÏß³Ì×ÊÔ´£»
Ï̳߳صĹرպÍÏú»Ù
[cpp] view plaincopy
void tp_close(TpThreadPool *pTp, TPBOOL wait) {
unsigned i;
pTp->stop_flag = TRUE;
if (wait) {
for (i = 0; i < pTp->cur_th_num; i++) {
pthread_cond_signal(&pTp->thread_info[i].thread_cond);
}
for (i = 0; i < pTp->cur_th_num; i++) {
pthread_join(pTp->thread_info[i].thread_id, NULL);
pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);
pthread_cond_destroy(&pTp->thread_info[i].thread_cond);
}
} else {
//close work thread
for (i = 0; i < pTp->cur_th_num; i++) {
kill((pid_t)pTp->thread_info[i].thread_id, SIGKILL);
pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);
pthread_cond_destroy(&pTp->thread_info[i].thread_cond);
}
}
//close manage thread
kill((pid_t)pTp->manage_thread_id, SIGKILL);
pthread_mutex_destroy(&pTp->tp_lock);
//free thread struct
free(pTp->thread_info);
pTp->thread_info = NULL;
}
Ï̳߳عرյĹý³ÌÖУ¬¿ÉÒÔÑ¡ÔñÊÇ·ñ¶ÔÕýÔÚ´¦ÀíµÄÈÎÎñ½øÐеȴý£¬Èç¹ûÊÇ£¬Ôò»á»½ÐÑËùÓÐÈÎÎñ£¬È»ºóµÈ´ýËùÓÐÈÎÎñÖ´ÐÐÍê³É£¬È»ºó·µ»Ø£»Èç¹û²»ÊÇ£¬Ôò½«Á¢¼´É±ËÀËùÓÐỊ̈߳¬È»ºó·µ»Ø£¬×¢Ò⣺Õâ¿ÉÄܻᵼÖÂÈÎÎñµÄ´¦ÀíÖж϶ø²úÉú´íÎó£¡
ÈÎÎñ´¦Àí
[cpp] view plaincopy
TPBOOL tp_process_job(TpThreadPool *pTp, process_job proc_fun, TpWorkDesc *job) {
TpThreadInfo *pThi ;
//fill pTp->thread_info's relative work key
pthread_mutex_lock(&pTp->tp_lock);
pThi = (TpThreadInfo *) deQueue(&pTp->idle_q);
pthread_mutex_unlock(&pTp->tp_lock);
if(pThi){
pThi->is_busy =TRUE;
pThi->proc_fun = proc_fun;
pThi->th_job = job;
pthread_cond_signal(&pThi->thread_cond);
DEBUG("Fetch a thread from pool.\n");
return TRUE;
}
//if all current thread are busy, new thread is created here
pthread_mutex_lock(&pTp->tp_lock);
pThi = tp_add_thread(pTp);
pthread_mutex_unlock(&pTp->tp_lock);
if(!pThi){
DEBUG("The thread pool is full, no more thread available.\n");
return FALSE;
}
DEBUG("No more idle thread, created a new one.\n");
pThi->proc_fun = proc_fun;
pThi->th_job = job;
//send cond to work thread
pthread_cond_signal(&pThi->thread_cond);
return TRUE;
}
µ±Ò»¸öÐÂÈÎÎñµ½´ïÊÇ£¬Ï̳߳ØÊ×ÏÈ»á¼ì²éÊÇ·ñÓпÉÓõĿÕÏÐỊ̈߳¬Èç¹ûÊÇ£¬Ôò²ÉÓòſÕÏÐÏ߳̽øÐÐÈÎÎñ´¦Àí²¢·µ»ØTRUE£¬Èç¹û²»ÊÇ£¬Ôò³¢ÊÔн¨Ò»¸öỊ̈߳¬²¢Ê¹ÓøÃÏ̶߳ÔÈÎÎñ½øÐд¦Àí£¬Èç¹ûʧ°ÜÔò·µ»ØFALSE£¬ËµÃ÷Ï̳߳Øæµ»òÕß³ö´í¡£
[cpp] view plaincopy
static void *tp_work_thread(void *arg) {
pthread_t curid;//current thread id
TpThreadInfo *pTinfo = (TpThreadInfo *) arg;
//wait cond for processing real job.
while (!(pTinfo->tp_pool->stop_flag)) {
pthread_mutex_lock(&pTinfo->thread_lock);
pthread_cond_wait(&pTinfo->thread_cond, &pTinfo->thread_lock);
pthread_mutex_unlock(&pTinfo->thread_lock);
//process
pTinfo->proc_fun(pTinfo->th_job);
//thread state be set idle after work
//pthread_mutex_lock(&pTinfo->thread_lock);
pTinfo->is_busy = FALSE;
enQueue(&pTinfo->tp_pool->idle_q, pTinfo);
//pthread_mutex_unlock(&pTinfo->thread_lock);
DEBUG("Job done, I am idle now.\n");
}
}
ÉÏÃæÕâ¸öº¯ÊýÊÇÈÎÎñ´¦Àíº¯Êý£¬¸Ãº¯Êý½«Ê¼ÖÕ´¦ÀíµÈ´ý»½ÐÑ״̬£¬Ö±µ½ÐÂÈÎÎñµ½´ï»òÕßÏß³ÌÏú»Ùʱ±»»½ÐÑ£¬È»ºóµ÷ÓÃÈÎÎñ´¦Àí»Øµ÷º¯Êý¶ÔÈÎÎñ½øÐд¦Àí£»µ±ÈÎÎñ´¦ÀíÍê³Éʱ£¬Ôò½«×Ô¼ºÖÃÈë¿ÕÏжÓÁÐÖУ¬ÒÔ¹©ÏÂÒ»¸öÈÎÎñ´¦Àí¡£
[cpp] view plaincopy
TpThreadInfo *tp_add_thread(TpThreadPool *pTp) {
int err;
TpThreadInfo *new_thread;
if (pTp->max_th_num <= pTp->cur_th_num)
return NULL;
//malloc new thread info struct
new_thread = pTp->thread_info + pTp->cur_th_num;
new_thread->tp_pool = pTp;
//init new thread's cond & mutex
pthread_cond_init(&new_thread->thread_cond, NULL);
pthread_mutex_init(&new_thread->thread_lock, NULL);
//init status is busy, only new process job will call this function
new_thread->is_busy = TRUE;
err = pthread_create(&new_thread->thread_id, NULL, tp_work_thread, new_thread);
if (0 != err) {
free(new_thread);
return NULL;
}
//add current thread number in the pool.
pTp->cur_th_num++;
return new_thread;
}
ÉÏÃæÕâ¸öº¯ÊýÓÃÓÚÏòÏ̳߳ØÖÐÌí¼ÓеÄỊ̈߳¬¸Ãº¯Êý½«»áÔÚµ±Ï̳߳ØûÓпÕÏÐÏ߳̿ÉÓÃʱ±»µ÷Óá£
º¯Êý½«»áн¨Ò»¸öỊ̈߳¬²¢ÉèÖÃ×Ô¼ºµÄ״̬Ϊbusy£¨Á¢¼´¾ÍÒª±»ÓÃÓÚÖ´ÐÐÈÎÎñ£©¡£
Ï̳߳عÜÀí
Ï̳߳صĹÜÀíÖ÷ÒªÊǼà¿ØÏ̳߳صÄÕûÌåæµ״̬£¬µ±Ï̳߳ش󲿷ÖÏ̴߳¦ÓÚ¿ÕÏÐ״̬ʱ£¬¹ÜÀíÏ߳̽«Êʵ±µÄÏú»ÙÒ»¶¨ÊýÁ¿µÄ¿ÕÏÐỊ̈߳¬ÒÔ±ã¼õÉÙÏ̳߳ضÔϵͳ×ÊÔ´µÄÏûºÄ¡£
ÕâÀïÉè¼ÆÈÏΪ£¬µ±¿ÕÏÐÏ̵߳ÄÊýÁ¿³¬¹ýÏ̳߳ØÏß³ÌÊýÁ¿µÄ1/2ʱ£¬Ï̳߳Ø×ÜÌå´¦Àí¿ÕÏÐ״̬£¬¿ÉÒÔÊʵ±Ïú»Ù²¿·ÖÏ̳߳صÄỊ̈߳¬ÒÔ¼õÉÙÏ̳߳ضÔϵͳ×ÊÔ´µÄ¿ªÏú¡£
Ï̳߳Ø״̬¼ÆËã
ÕâÀïµÄBUSY_THRESHOLDµÄÖµÊÇ0.5£¬Ò²¼´Êǵ±¿ÕÏÐÏß³ÌÊýÁ¿³¬¹ýÒ»°ëʱ£¬·µ»Ø0£¬ËµÃ÷Ï̳߳ØÕûÌå״̬ΪÏУ¬·ñÔò·µ»Ø1£¬ËµÃ÷Ϊæ¡£
[cpp] view plaincopy
int tp_get_tp_status(TpThreadPool *pTp) {
float busy_num = 0.0;
int i;
//get busy thread number
busy_num = pTp->cur_th_num - pTp->idle_q.count;
DEBUG("Current thread pool status, current num: %u, busy num: %u, idle num: %u\n", pTp->cur_th_num, (unsigned)busy_num, pTp->idle_q.count);
//0.2? or other num?
if (busy_num / (pTp->cur_th_num) < BUSY_THRESHOLD)
return 0;//idle status
else
return 1;//busy or normal status
}
Ï̵߳ÄÏú»ÙËã·¨
1. ´Ó¿ÕÏжÓÁÐÖÐdequeueÒ»¸ö¿ÕÏÐÏß³ÌÖ¸Õ룬¸ÃÖ¸ÕëÖ¸ÏòÏß³ÌÐÅÏ¢Êý×éµÄijÏÀýÈçÕâÀïÊÇp£»
2. Ïú»Ù¸ÃÏß³Ì
3. °ÑÏß³ÌÐÅÏ¢Êý×éµÄ×îºóÒ»Ï±´ÖÁλÖÃp
4. Ï̳߳ØÊýÁ¿¼õÉÙÒ»£¬¼´cur_th_num--
[cpp] view plaincopy
TPBOOL tp_delete_thread(TpThreadPool *pTp) {
unsigned idx;
TpThreadInfo *pThi;
TpThreadInfo tT;
//current thread num can't < min thread num
if (pTp->cur_th_num <= pTp->min_th_num)
return FALSE;
//pthread_mutex_lock(&pTp->tp_lock);
pThi = deQueue(&pTp->idle_q);
//pthread_mutex_unlock(&pTp->tp_lock);
if(!pThi)
return FALSE;
//after deleting idle thread, current thread num -1
pTp->cur_th_num--;
memcpy(&tT, pThi, sizeof(TpThreadInfo));
memcpy(pThi, pTp->thread_info + pTp->cur_th_num, sizeof(TpThreadInfo));
//kill the idle thread and free info struct
kill((pid_t)tT.thread_id, SIGKILL);
pthread_mutex_destroy(&tT.thread_lock);
pthread_cond_destroy(&tT.thread_cond);
return TRUE;
}
Ï̳߳ؼà¿Ø
Ï̳߳Øͨ¹ýÒ»¸ö¹ÜÀíÏß³ÌÀ´½øÐмà¿Ø£¬¹ÜÀíÏ߳̽«»áÿ¸ôÒ»¶Îʱ¼ä¶ÔÏ̳߳صÄ״̬½øÐмÆË㣬¸ù¾ÝÏ̳߳صÄ״̬Êʵ±µÄÏú»Ù²¿·ÖỊ̈߳¬¼õÉÙ¶Ôϵͳ×ÊÔ´µÄÏûºÄ¡£
[cpp] view plaincopy
static void *tp_manage_thread(void *arg) {
TpThreadPool *pTp = (TpThreadPool*) arg;//main thread pool struct instance
//1?
sleep(MANAGE_INTERVAL);
do {
if (tp_get_tp_status(pTp) == 0) {
do {
if (!tp_delete_thread(pTp))
break;
} while (TRUE);
}//end for if
//1?
sleep(MANAGE_INTERVAL);
} while (!pTp->stop_flag);
return NULL;
}
³ÌÐò²âÊÔ
ÖÁ´Ë£¬ÎÒÃǵÄÉè¼ÆÐèҪʹÓÃÒ»¸ö²âÊÔ³ÌÐòÀ´½øÐÐÑéÖ¤¡£ÓÚÊÇ£¬ÎÒÃÇдÏÂÕâÑùÒ»¶Î´úÂë¡£
[cpp] view plaincopy
#include
#include
#include "thread_pool.h"
#define THD_NUM 10
void proc_fun(TpWorkDesc *job){
int i;
int idx=*(int *)job->arg;
printf("Begin: thread %d\n", idx);
sleep(3);
printf("End: thread %d\n", idx);
}
int main(int argc, char **argv){
TpThreadPool *pTp= tp_create(5,10);
TpWorkDesc pWd[THD_NUM];
int i, *idx;
tp_init(pTp);
for(i=0; i < THD_NUM; i++){
idx=(int *) malloc(sizeof(int));
*idx=i;
pWd[i].arg=idx;
tp_process_job(pTp, proc_fun, pWd+i);
usleep(400000);
}
//sleep(1);
tp_close(pTp, TRUE);
free(pTp);
printf("All jobs done!\n");
return 0;
}
Ô´ÂëÏÂÔصØÖ·£ºhttps://sourceforge.net/projects/thd-pool-linux/