- 论坛徽章:
- 0
|
void tp_process_job(tp_thread_pool *this, tp_work *worker, tp_work_desc *job){
if(!this->schedule_job(this,worker,job))
{
do
usleep(5);
while(!this->schedule_job(this,worker,job));
}
return;
}
/**
* member function reality. get real thread by thread id num.
* para:
* this: thread pool struct instance ponter
* id: thread id num
* return:
* seq num in thread info struct array
*/
int tp_get_thread_by_id(tp_thread_pool *this, int id){
int i;
for(i=0;i<this->cur_th_num;i++){
if(id == this->thread_info[i].thread_id)
return i;
}
return -1;
}
/**
* 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 NULL;
printf("entering working thread %d, thread id is %u\n", nseq, (unsigned int)curid);
//wait cond for processing real job.
while( TRUE ){
pthread_mutex_lock(&this->thread_info[nseq].thread_lock);
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("thread %u do work!\n",(unsigned int)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("thread %u do work over\n", (unsigned int)pthread_self());
}
}
void testtpool(tp_work *worker,tp_work_desc *job)
{
struct value_pass *value_pa;
value_pa=(struct value_pass*)malloc(sizeof(struct value_pass));
value_pa->first=job->chnum;
value_pa->second=job->inum;
//value_pa->first++;
printf("thread %u excute,first value is %d,second value is %s\n",( unsigned int)pthread_self(),value_pa->first,value_pa->second);
free(value_pa);
usleep(3);
return ;
}
int main()
{
struct value_pass *value_pa;
tp_work *worker;
tp_work_desc *job;
job=(tp_work_desc*)malloc(sizeof(tp_work_desc));
worker=(tp_work *)malloc(sizeof(tp_work));
char secondvalue[5]="abcd";
int value=0;
tp_thread_pool *this;
this=creat_thread_pool(2);
tp_init(this);
for (;;)
{
value_pa=(struct value_pass*)malloc(sizeof(struct value_pass));
memset(value_pa,0,sizeof(struct value_pass));
value_pa->first=value++;
value_pa->second=secondvalue;
memset(job,0,sizeof(tp_work_desc));
memset(worker,0,sizeof( tp_work));
job->inum=value_pa->second;
job->chnum=value_pa->first;
free(value_pa);
worker->process_job=testtpool;
tp_process_job(this,worker,job);
usleep(1);
}
free(job);
free(worker);
tp_close(this);
return 1;
}
|
运行一段时间后(有时长,有时短),程序停止。在工作队列的线程等不到唤醒,一直在等待。不知不对导致的,大家帮忙看看。 |
|