使用二个互斥量的多线程程序.doc

上传人:re****.1 文档编号:547410304 上传时间:2022-12-10 格式:DOC 页数:6 大小:161.50KB
返回 下载 相关 举报
使用二个互斥量的多线程程序.doc_第1页
第1页 / 共6页
使用二个互斥量的多线程程序.doc_第2页
第2页 / 共6页
使用二个互斥量的多线程程序.doc_第3页
第3页 / 共6页
使用二个互斥量的多线程程序.doc_第4页
第4页 / 共6页
使用二个互斥量的多线程程序.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《使用二个互斥量的多线程程序.doc》由会员分享,可在线阅读,更多相关《使用二个互斥量的多线程程序.doc(6页珍藏版)》请在金锄头文库上搜索。

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;s

2、truct 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(&hashloc

3、k);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

4、=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_l

5、ock);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(&

6、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_

7、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;f

8、p = 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

9、_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

10、(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(p

11、thread_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 pthr

12、ead_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

13、_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);return -1;struct foo *fp = NULL;int cnt = 0;for (i = 0; i f_id, fp-f_count);for ( j = 0; j visitedj);printf(n);fp = fp-f_next;cnt+;printf(there are %d foos totallyn, cnt);printf(main endn);return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号