




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include#include#include#include#include#define HASH(id) (id) % NHASH)#define THREAD_NUM 5#define FOO_NUM 10#define NHASH 29struct foo *fhNHASH;pthread_mutex_t hashlock=PTHREAD_MUTEX_INITIALIZER;struct fooint f_count;pthread_mutex_t f_lock;struct foo *f_next;int f_id;int visitedTHREAD_NUM;int vnum;struct foo* foo_alloc(int fid)struct foo *fp;int i, idx;if(fp=malloc(sizeof(struct foo)!=NULL)fp-f_count=1;if(pthread_mutex_init(&fp-f_lock,NULL)!=0)free(fp);return NULL;idx=HASH(fid);pthread_mutex_lock(&hashlock);fp-f_next = fhidx;fhidx=fp;pthread_mutex_lock(&fp-f_lock);pthread_mutex_unlock(&hashlock);for (i = 0; i THREAD_NUM; i+) fp-visitedi = -1;pthread_mutex_unlock(&fp-f_lock);return fp;void foo_hold(int tid, struct foo *fp)pthread_mutex_lock(&fp-f_lock);fp-f_count+;fp-visitedfp-vnum+ = tid;pthread_mutex_unlock(&fp-f_lock);struct foo * foo_find(int tid, int foo_id)struct foo *fp;int idx;idx=HASH(foo_id);pthread_mutex_lock(&hashlock);for(fp=fhidx;fp!=NULL;fp=fp-f_next)if(fp-f_id=foo_id)foo_hold(tid, fp);break;pthread_mutex_unlock(&hashlock);return fp;void foo_rele(int fid, struct foo *fp)struct foo *tfp;int idx;pthread_mutex_lock(&fp-f_lock);if(fp-f_count=1)pthread_mutex_unlock(&fp-f_lock);pthread_mutex_lock(&hashlock);pthread_mutex_lock(&fp-f_lock);if(fp-f_count!=1)fp-f_count-;pthread_mutex_unlock(&fp-f_lock);pthread_mutex_unlock(&hashlock);return;idx=HASH(fid);tfp=fhidx;if(tfp=fp)fhidx=fp-f_next;elsewhile(tfp-f_next!=fp)tfp=tfp-f_next;tfp-f_next=fp-f_next;pthread_mutex_unlock(&hashlock);pthread_mutex_unlock(&fp-f_lock);pthread_mutex_destroy(&fp-f_lock);free(fp); else fp-f_count-;pthread_mutex_unlock(&fp-f_lock);void* thrd_func_0(void *arg) int tid = (int)arg;int i,j,foo_id;i = j = foo_id = 0;struct foo *fp = NULL;for (i = 0; i THREAD_NUM;i+)for (j = 0; j FOO_NUM; j+) foo_id = j + i * FOO_NUM;fp = foo_find(tid, foo_id);if (NULL != fp) foo_rele(foo_id, fp);usleep(100);return NULL;void* thrd_func_1(void *arg) int tid = (int)arg;int i,j,foo_id;i = j = foo_id = 0;struct foo *fp = NULL;for (i = THREAD_NUM - 1; i = 0;i-)for (j = 0; j FOO_NUM; j+) foo_id = j + i * FOO_NUM;fp = foo_find(tid, foo_id);if (NULL != fp) foo_rele(foo_id, fp);usleep(1000);return NULL;void* thrd_func_2(void *arg) int tid = (int)arg;int i,j,foo_id;i = j = foo_id = 0;struct foo *fp = NULL;for (j = 0; j FOO_NUM; j+)for (i = THREAD_NUM - 1; i = 0;i-) foo_id = j + i * FOO_NUM;fp = foo_find(tid, foo_id);if (NULL != fp) foo_rele(foo_id, fp);usleep(1000);return NULL;void* thrd_func_3(void *arg) int tid = (int)arg;int i,j,foo_id;i = j = foo_id = 0;struct foo *fp = NULL;for (i = 0; i NHASH;i+) fp = fhi;while (NULL != fp) foo_id = fp-f_id;foo_hold(tid, fp);foo_rele(foo_id, fp);fp = fp-f_next;usleep(1000);return NULL;void* thrd_func_4(void *arg) int tid = (int)arg;int i,j,foo_id;i = j = foo_id = 0;struct foo *fp = NULL;for (i = NHASH - 1; i = 0;i-) fp = fhi;while (NULL != fp) foo_id = fp-f_id;foo_hold(tid, fp);foo_rele(foo_id, fp);fp = fp-f_next;usleep(1000);return NULL;int my_pthread_create(pthread_t *tid, int i)switch(i)case 0:return pthread_create(tid, NULL, thrd_func_0, (void*)i);case 1:return pthread_create(tid, NULL, thrd_func_1, (void*)i);case 2:return pthread_create(tid, NULL, thrd_func_2, (void*)i);case 3:return pthread_create(tid, NULL, thrd_func_3, (void*)i);case 4:return pthread_create(tid, NULL, thrd_func_4, (void*)i);return 0;int main()int i, j, fid, err;pthread_t tidsTHREAD_NUM;void* tret = NULL;i = j = fid = err = 0;printf(main startn);for (i = 0; i THREAD_NUM;i+)for (j = 0; j FOO_NUM; j+) fid = j + i * FOO_NUM;foo_alloc(fid);for ( i = 0; i THREAD_NUM; i+ ) err = my_pthread_create(&tidsi, i);if (0 != err) printf(pthread_create failedn);return -1;for ( i = 0; i THREAD_NUM; i+ ) err = pthread_join(tidsi, &tret);if (0 != err) printf(pthread_join failedn)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年东北农业大学专职辅导员公开招聘16人模拟试卷附答案详解(突破训练)
- 2025黑龙江省机关事务管理局所属事业单位招聘工作人员10人考前自测高频考点模拟试题及答案详解(有一套)
- 2025广东韶关市新丰县招聘暨选聘公办教师30人(编制)考前自测高频考点模拟试题及答案详解1套
- 美国的时间轴课件
- 2025商务合同范本购买协议
- 2025电影院加盟协议
- 福彩系统培训的安全技能课件
- 第15课 货币的使用与世界货币体系的形成说课稿高中历史选择性必修1 国家制度与社会治理统编版(部编版)
- 2025工程咨询公司合作协议格式
- 禁毒安全知识教育培训课件
- 思政家乡课件
- 2025年职业培训学校建设项目可行性分析与初步设计方案报告
- 2025年软件架构师专业技术考核试题及答案解析
- 八上语文第9课《天上有颗南仁东星》课件
- 2024年BRCGS包装材料全球标准第7版全套管理手册及程序文件(可编辑)
- 2023年上海市虹口区初三一模语文试卷(含答案)
- 优势视角课件完整版
- 花城版音乐课时15-第12课 走近戏曲(一)观赏京剧学习念白-京剧丑角的念白《报灯名》-课件
- 《食品安全法》与粮食质量安全专题培训课件
- 2023年安康市交通建设投资集团有限公司招聘笔试题库及答案解析
- 文理分科心理测试问卷
评论
0/150
提交评论