该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
for (i = 0; i < this->cur_th_num; i++) { if (id == this->thread_info[i].thread_id) return i; }
return -1;}
/** * member function reality. add new thread into the pool. * para: * this: thread pool struct instance ponter * return: * true: successful; false: failed */static TPBOOL tp_add_thread(tp_thread_pool *this) { int err; tp_thread_info *new_thread;
if (this->max_th_num <= this->cur_th_num) return FALSE;
//malloc new thread info struct new_thread = &this->thread_info[this->cur_th_num];
//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 new_thread->is_busy = TRUE;
//add current thread number in the pool. this->cur_th_num++;
err = pthread_create(&new_thread->thread_id, NULL, tp_work_thread, this); if (0 != err) { free(new_thread); return FALSE; } /* printf("tp_add_thread: 创建工作线程 %d\n", (int) (this->thread_info[this->cur_th_num - 1].thread_id)); */
return TRUE;}
/** * member function reality. delete idle thread in the pool. * only delete last idle thread in the pool. * para: * this: thread pool struct instance ponter * return: * true: successful; false: failed */static TPBOOL tp_delete_thread(tp_thread_pool *this) { //current thread num can't < min thread num if (this->cur_th_num <= this->min_th_num) return FALSE;
//if last thread is busy, do nothing if (this->thread_info[this->cur_th_num - 1].is_busy) return FALSE;
//kill the idle thread and free info struct kill(this->thread_info[this->cur_th_num - 1].thread_id, SIGKILL); pthread_mutex_destroy(&this->thread_info[this->cur_th_num - 1].thread_lock); pthread_cond_destroy(&this->thread_info[this->cur_th_num - 1].thread_cond);
//after deleting idle thread, current thread num -1 this->cur_th_num--;
return TRUE;}
/** * member function reality. get current thread pool status:idle, normal, busy, .etc. * para: * this: thread pool struct instance ponter * return: * 0: idle; 1: normal or busy(don't process) */static int tp_get_tp_status(tp_thread_pool *this) { float busy_num = 0.0; int i;
//get busy thread number for (i = 0; i < this->cur_th_num; i++) { if (this->thread_info[i].is_busy) busy_num++; }
//0.2? or other num? if (busy_num / (this->cur_th_num) < BUSY_THRESHOLD) return 0; //idle status else return 1; //busy or normal status}
/** * internal interface. real work thread. * para: * pthread: thread pool struct ponter * return: */static void *tp_work_thread(void *pthread) { pthread_t curid; //current thread id int nseq; //current thread seq in the this->thread_info array tp_thread_pool *this = (tp_thread_pool*) pthread; //main thread pool struct instance
//get current thread id curid = pthread_self();
//get current thread's seq in the thread info struct array. nseq = this->get_thread_by_id(this, curid); if (nseq < 0) return; /* printf("进入工作线程 %d, 线程 id 为 %d\n", nseq, curid); */
//wait cond for processing real job. while (TRUE) { pthread_mutex_lock(&this->thread_info[nseq].thread_lock); this->thread_info[nseq].is_busy = FALSE;
pthread_cond_wait(&this->thread_info[nseq].thread_cond, &this->thread_info[nseq].thread_lock); pthread_mutex_unlock(&this->thread_info[nseq].thread_lock);
/* printf("线程 %d 工作!\n", pthread_self()); */
tp_work *work = this->thread_info[nseq].th_work; tp_work_desc *job = this->thread_info[nseq].th_job;
//process work->process_job(work, job);
//thread state be set idle after work /* pthread_mutex_lock(&this->thread_info[nseq].thread_lock); this->thread_info[nseq].is_busy = FALSE; pthread_mutex_unlock(&this->thread_info[nseq].thread_lock); */
/* printf("线程 %d 工作结束\n", pthread_self()); */ }}
/** * internal interface. manage thread pool to delete idle thread. * para: * pthread: thread pool struct ponter * return: */static void *tp_manage_thread(void *pthread) { tp_thread_pool *this = (tp_thread_pool*) pthread; //main thread pool struct instance
sleep(MANAGE_INTERVAL);
do { if (this->get_tp_status(this) == 0) { do { if (!this->delete_thread(this)) break; } while (TRUE); }//end for if
sleep(MANAGE_INTERVAL); } while (TRUE);}