资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
#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);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;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号