VB线程池.DOCVIP

  1. 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
VB线程池

#include #include #include #include #include #include #include #include #define SIZE 50 sem_t sem1,sem2,sem3; //sem1:线程个数,sem2:要执行任务个数,sem3 排队任务总数 struct process //任务结构 { int (*process_fun)(const char *); //任务函数 char process_arg[SIZE]; //任务参数 }; struct pthreadnode //线程结构 { struct process *pth_pt; //线程任务 pthread_t pth_id; //线程ID int pth_flag; //线程使用标志 pthread_mutex_t pth_mutex; //线程锁 }; struct lake //线程池 { struct process *lake_prohead; //任务排队列表,这里申请为数组头结点 int w; //w 后续任务插入位置,r为将要执行任务位置 int r; int prono; //排队任务的个数,即lake_prohead数组大小 struct pthreadnode *lake_pthhead; //线程列表, 这里申请为数组头结点 int lake_pthno; //线程个数 pthread_t lake_id; //线程池 id }; /*------------任务添加函数---------*/ /* T 为线程池指针 process_fun 为任务函数 process_arg 任务参数 */ void fun_add(struct lake *T,int (*process_fun)(const char *),const char *process_arg) { (T-lake_prohead+T-w)-process_fun=process_fun; memcpy((T-lake_prohead+T-w)-process_arg,process_arg,SIZE); ++(T-w); if(T-w == T-prono) { T-w=0; } } /*------------线程清理函数---------*/ void clean(void *p) { pthread_mutex_unlock((pthread_mutex_t *)p); } /*------------线程执行函数---------*/ void *pthread_fun(void *arg) { struct pthreadnode *tmp=(struct pthreadnode *)arg; pthread_cleanup_push(clean,tmp-pth_mutex); while(1) { pthread_mutex_lock(tmp-pth_mutex); //阻塞,等待任务到来 printf(%x do %s\n,pthread_self(),tmp-pth_pt-process_arg); tmp-pth_pt-process_fun(tmp-pth_pt-process_arg); //执行任务 tmp-pth_flag=0; sem_post(sem3); //任务完成,可以空出一个等待任务栏 sem_post(sem1); //线程使用完毕,空出一个线程 } pthread_cleanup_pop(1); pthread_exit(0); } /*------------线程执行函数---------*/ void *lake_goon(void *p) { int i=0; struct lake *T=(struct lake *)p; while(1) { sem_wait(sem1); //等待线程是否有空的,无就等待 sem_wait(sem2); //等待是否有要执行的函数,无就等待 for(i=0;ilake_pthno;++i) //当有线程空有执行函数时 ,查找空闲线程 { if((T-lake_pthhead+i)-pth_flag == 0) { break; } } (T-lake_pthhead+i)-pth_flag=1; //找到空闲线程并置1 (T-lake_pthhead+i)-pth_pt=(T-lake_prohead+T-r); //线程任务 指向任务排队列表,r所在任务 ++ (T-r); //任务下个 if(T-r == T-prono) {

文档评论(0)

baoyue + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档