使用二个互斥量的多线程程序.doc_第1页
使用二个互斥量的多线程程序.doc_第2页
使用二个互斥量的多线程程序.doc_第3页
使用二个互斥量的多线程程序.doc_第4页
使用二个互斥量的多线程程序.doc_第5页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论