免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2502 | 回复: 5
打印 上一主题 下一主题

线程池程序,好像死锁了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-17 07:36 |只看该作者 |倒序浏览
一份小线程池代码,见代码:
#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <pthread.h>

#include <signal.h>

#include <string.h>

#include <unistd.h>



#ifndef TPBOOL

typedef int TPBOOL;

#endif



#ifndef TRUE

#define TRUE 1

#endif



#ifndef FALSE

#define FALSE 0

#endif







typedef struct tp_work_desc_s tp_work_desc;

typedef struct tp_work_s tp_work;

typedef struct tp_thread_info_s tp_thread_info;

typedef struct tp_thread_pool_s tp_thread_pool;



//thread parm


struct tp_work_desc_s{

&nbsp;&nbsp;&nbsp;&nbsp;char *inum;&nbsp;&nbsp;&nbsp;&nbsp;//call in


&nbsp;&nbsp;&nbsp;&nbsp;char *onum;&nbsp;&nbsp;&nbsp;&nbsp;//call out


&nbsp;&nbsp;&nbsp;&nbsp;int chnum;&nbsp;&nbsp;&nbsp;&nbsp;//channel num


};



//base thread struct


struct tp_work_s{

&nbsp;&nbsp;&nbsp;&nbsp;//main process function. user interface


&nbsp;&nbsp;&nbsp;&nbsp;void (*process_job)(tp_work *this, tp_work_desc *job);

};



//thread info


struct tp_thread_info_s{

&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread_id;&nbsp;&nbsp;&nbsp;&nbsp;//thread id num


&nbsp;&nbsp;&nbsp;&nbsp;TPBOOL  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is_busy;&nbsp;&nbsp;&nbsp;&nbsp;//thread status:true-busy;flase-idle


&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_t          thread_cond;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread_lock;

&nbsp;&nbsp;&nbsp;&nbsp;tp_work&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*th_work;

&nbsp;&nbsp;&nbsp;&nbsp;tp_work_desc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*th_job;

};



//main thread pool struct


struct tp_thread_pool_s{

&nbsp;&nbsp;&nbsp;&nbsp;TPBOOL (*init)(tp_thread_pool *this);

&nbsp;&nbsp;&nbsp;&nbsp;void (*close)(tp_thread_pool *this);

&nbsp;&nbsp;&nbsp;&nbsp;void (*process_job)(tp_thread_pool *this, tp_work *worker, tp_work_desc *job);

&nbsp;&nbsp;&nbsp;&nbsp;int  (*get_thread_by_id)(tp_thread_pool *this, int id);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TPBOOL (*schedule_job)(tp_thread_pool *this,tp_work *worker,tp_work_desc *job);

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;int cur_th_num;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//current thread number in the pool


&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_t tp_lock;

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;tp_thread_info *thread_info;&nbsp;&nbsp;&nbsp;&nbsp;//work thread relative thread info


};



tp_thread_pool *creat_thread_pool(int cur_num);
TPBOOL tp_init(tp_thread_pool *this);
void tp_process_job(tp_thread_pool *this, tp_work *worker, tp_work_desc *job);

论坛徽章:
0
2 [报告]
发表于 2009-12-17 07:44 |只看该作者
#include "thread-pool.h"

static void *tp_work_thread(void *pthread);

static void tp_close(tp_thread_pool *this);

static int  tp_get_thread_by_id(tp_thread_pool *this, int id);
static TPBOOL tp_schedule_job(tp_thread_pool *this,tp_work *worker,tp_work_desc *job);

struct value_pass{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int first;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *second;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};



/**

&nbsp;&nbsp;* user interface. creat thread pool.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;num: min thread number to be created in the pool

&nbsp;&nbsp;* return:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;thread pool struct instance be created successfully

&nbsp;&nbsp;*/


tp_thread_pool *creat_thread_pool(int cur_num){

&nbsp;&nbsp;&nbsp;&nbsp;tp_thread_pool *this;

&nbsp;&nbsp;&nbsp;&nbsp;this = (tp_thread_pool*)malloc(sizeof(tp_thread_pool));&nbsp;&nbsp;&nbsp;&nbsp;



&nbsp;&nbsp;&nbsp;&nbsp;memset(this, 0, sizeof(tp_thread_pool));

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;//init member function ponter


&nbsp;&nbsp;&nbsp;&nbsp;this->init = tp_init;

&nbsp;&nbsp;&nbsp;&nbsp;this->close = tp_close;

&nbsp;&nbsp;&nbsp;&nbsp;this->process_job = tp_process_job;

&nbsp;&nbsp;&nbsp;&nbsp;this->get_thread_by_id = tp_get_thread_by_id;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this->schedule_job = tp_schedule_job;

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;this->cur_th_num = cur_num;

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_init(&this->tp_lock, NULL);



&nbsp;&nbsp;&nbsp;&nbsp;//malloc mem for num thread info struct


&nbsp;&nbsp;&nbsp;&nbsp;if(NULL != this->thread_info)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(this->thread_info);

&nbsp;&nbsp;&nbsp;&nbsp;this->thread_info = (tp_thread_info*)malloc(sizeof(tp_thread_info)*this->cur_th_num);

&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;return this;

}





/**

&nbsp;&nbsp;* member function reality. thread pool init function.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;this: thread pool struct instance ponter

&nbsp;&nbsp;* return:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;true: successful; false: failed

&nbsp;&nbsp;*/


TPBOOL tp_init(tp_thread_pool *this){

&nbsp;&nbsp;&nbsp;&nbsp;int i;

&nbsp;&nbsp;&nbsp;&nbsp;int err;

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;//creat work thread and init work thread info


&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i<this->cur_th_num;i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_init(&this->thread_info[i].thread_cond, NULL);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_init(&this->thread_info[i].thread_lock, NULL);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = pthread_create(&this->thread_info[i].thread_id, NULL, tp_work_thread, this);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(0 != err){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("tp_init: creat work thread failed\n");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("tp_init: creat work thread %u\n", (unsigned int)this->thread_info[i].thread_id);

&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;

}



/**

&nbsp;&nbsp;* member function reality. thread pool entirely close function.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;this: thread pool struct instance ponter

&nbsp;&nbsp;* return:

&nbsp;&nbsp;*/


void tp_close(tp_thread_pool *this){

&nbsp;&nbsp;&nbsp;&nbsp;int i;

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;//close work thread


&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i<this->cur_th_num;i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kill(this->thread_info[i].thread_id, SIGKILL);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_destroy(&this->thread_info[i].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_destroy(&this->thread_info[i].thread_cond);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("tp_close: kill work thread %u\n", (unsigned int)this->thread_info[i].thread_id);

&nbsp;&nbsp;&nbsp;&nbsp;}



&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;free(this->thread_info);

}


TPBOOL tp_schedule_job(tp_thread_pool *this,tp_work *worker,tp_work_desc *job)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i<this->cur_th_num;i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_lock(&this->thread_info[i].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!this->thread_info[i].is_busy){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("tp_process_job:thread %d idle, thread id is %u\n", i, (unsigned int)this->thread_info[i].thread_id);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//thread state be set busy before work


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;this->thread_info[i].is_busy = TRUE;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&this->thread_info[i].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this->thread_info[i].th_work = worker;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this->thread_info[i].th_job = job;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("tp_process_job: informing idle working thread %d, thread id is %u\n", i, (unsigned int)this->thread_info[i].thread_id);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_signal(&this->thread_info[i].thread_cond);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&this->thread_info[i].thread_lock);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;}//end of for  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;
}



/**

&nbsp;&nbsp;* member function reality. main interface opened.

&nbsp;&nbsp;* after getting own worker and job, user may use the function to process the task.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;this: thread pool struct instance ponter

&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;worker: user task reality.

&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;job: user task para

&nbsp;&nbsp;* return:

&nbsp;&nbsp;*/

论坛徽章:
0
3 [报告]
发表于 2009-12-17 07:47 |只看该作者
void tp_process_job(tp_thread_pool *this, tp_work *worker, tp_work_desc *job){

&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!this->schedule_job(this,worker,job))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;   do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usleep(5);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(!this->schedule_job(this,worker,job));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;

&nbsp;&nbsp;&nbsp;&nbsp;

}



/**

&nbsp;&nbsp;* member function reality. get real thread by thread id num.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;this: thread pool struct instance ponter

&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;id: thread id num

&nbsp;&nbsp;* return:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;seq num in thread info struct array

&nbsp;&nbsp;*/


int tp_get_thread_by_id(tp_thread_pool *this, int id){

&nbsp;&nbsp;&nbsp;&nbsp;int i;



&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i<this->cur_th_num;i++){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(id == this->thread_info[i].thread_id)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return i;

&nbsp;&nbsp;&nbsp;&nbsp;}



&nbsp;&nbsp;&nbsp;&nbsp;return -1;

}



/**

&nbsp;&nbsp;* internal interface. real work thread.

&nbsp;&nbsp;* para:

&nbsp;&nbsp;* &nbsp;&nbsp;&nbsp;&nbsp;pthread: thread pool struct ponter

&nbsp;&nbsp;* return:

&nbsp;&nbsp;*/


static void *tp_work_thread(void *pthread){

&nbsp;&nbsp;&nbsp;&nbsp;pthread_t curid;//current thread id


&nbsp;&nbsp;&nbsp;&nbsp;int nseq;//current thread seq in the this->thread_info array


&nbsp;&nbsp;&nbsp;&nbsp;tp_thread_pool *this = (tp_thread_pool*)pthread;//main thread pool struct instance




&nbsp;&nbsp;&nbsp;&nbsp;//get current thread id


&nbsp;&nbsp;&nbsp;&nbsp;curid = pthread_self();

&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;//get current thread's seq in the thread info struct array.


&nbsp;&nbsp;&nbsp;&nbsp;nseq = this->get_thread_by_id(this, curid);

&nbsp;&nbsp;&nbsp;&nbsp;if(nseq < 0)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;

&nbsp;&nbsp;&nbsp;&nbsp;printf("entering working thread %d, thread id is %u\n", nseq, (unsigned int)curid);



&nbsp;&nbsp;&nbsp;&nbsp;//wait cond for processing real job.


&nbsp;&nbsp;&nbsp;&nbsp;while( TRUE ){

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_lock(&this->thread_info[nseq].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_cond_wait(&this->thread_info[nseq].thread_cond, &this->thread_info[nseq].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&this->thread_info[nseq].thread_lock);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("thread %u do work!\n",(unsigned int)pthread_self());



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp_work *work = this->thread_info[nseq].th_work;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp_work_desc *job = this->thread_info[nseq].th_job;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//process


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;work->process_job(work, job);



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//thread state be set idle after work


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_lock(&this->thread_info[nseq].thread_lock);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this->thread_info[nseq].is_busy = FALSE;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pthread_mutex_unlock(&this->thread_info[nseq].thread_lock);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("thread %u do work over\n", (unsigned int)pthread_self());

&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;

}





void testtpool(tp_work *worker,tp_work_desc *job)

{

&nbsp;&nbsp;&nbsp;&nbsp;struct value_pass *value_pa;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value_pa=(struct value_pass*)malloc(sizeof(struct value_pass));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value_pa->first=job->chnum;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value_pa->second=job->inum;

&nbsp;&nbsp;&nbsp;&nbsp;//value_pa->first++;


&nbsp;&nbsp;&nbsp;&nbsp;printf("thread %u excute,first value is %d,second value is %s\n",( unsigned int)pthread_self(),value_pa->first,value_pa->second);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(value_pa);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usleep(3);

&nbsp;&nbsp;&nbsp;&nbsp;return ;

}



int main()

{&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;struct value_pass *value_pa;

&nbsp;&nbsp;&nbsp;&nbsp; tp_work *worker;

&nbsp;&nbsp;&nbsp;&nbsp; tp_work_desc *job;

&nbsp;&nbsp;&nbsp;&nbsp; job=(tp_work_desc*)malloc(sizeof(tp_work_desc));

&nbsp;&nbsp;&nbsp;&nbsp;worker=(tp_work *)malloc(sizeof(tp_work));

&nbsp;&nbsp;&nbsp;&nbsp;char secondvalue[5]="abcd";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int value=0;

&nbsp;&nbsp;&nbsp;&nbsp;tp_thread_pool *this;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this=creat_thread_pool(2);

&nbsp;&nbsp;&nbsp;&nbsp;tp_init(this);

&nbsp;&nbsp;&nbsp;&nbsp;for (;;)

&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value_pa=(struct value_pass*)malloc(sizeof(struct value_pass));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(value_pa,0,sizeof(struct value_pass));

&nbsp;&nbsp;&nbsp;&nbsp;        value_pa->first=value++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value_pa->second=secondvalue;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(job,0,sizeof(tp_work_desc));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(worker,0,sizeof( tp_work));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job->inum=value_pa->second;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job->chnum=value_pa->first;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(value_pa);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worker->process_job=testtpool;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp_process_job(this,worker,job);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usleep(1);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(job);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(worker);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tp_close(this);

&nbsp;&nbsp;&nbsp;&nbsp;return 1;

}


运行一段时间后(有时长,有时短),程序停止。在工作队列的线程等不到唤醒,一直在等待。不知不对导致的,大家帮忙看看。
九片_cu 该用户已被删除
4 [报告]
发表于 2009-12-17 12:21 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
九片_cu 该用户已被删除
5 [报告]
发表于 2009-12-17 12:25 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2009-12-18 15:45 |只看该作者
谢谢,不是这样的原因。
没人帮忙解答下么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP