首页 使用二个互斥量的多线程程序

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

举报
开通vip

使用二个互斥量的多线程程序使用二个互斥量的多线程程序 #include#include #include #include #include #define HASH(id) ((id) % NHASH) #define THREAD_NUM 5 #define FOO_NUM 10 #define NHASH 29 struct foo *fh[NHASH]; pthread_mutex_t hashlock=PTHREAD_MUTEX_INITIALIZER; struct foo{ int f_count; p...

使用二个互斥量的多线程程序
使用二个互斥量的多线程程序 #include#include #include #include #include #define HASH(id) ((id) % NHASH) #define THREAD_NUM 5 #define FOO_NUM 10 #define NHASH 29 struct foo *fh[NHASH]; pthread_mutex_t hashlock=PTHREAD_MUTEX_INITIALIZER; struct foo{ int f_count; pthread_mutex_t f_lock; struct foo *f_next; int f_id; int visited[THREAD_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 = fh[idx]; fh[idx]=fp; pthread_mutex_lock(&fp->f_lock); pthread_mutex_unlock(&hashlock); for (i = 0; i < THREAD_NUM; i++) { fp->visited[i] = -1; } pthread_mutex_unlock(&fp->f_lock); return fp; advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts } void foo_hold(int tid, struct foo *fp) { pthread_mutex_lock(&fp->f_lock); fp->f_count++; fp->visited[fp->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=fh[idx];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=fh[idx]; if(tfp==fp){ advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts fh[idx]=fp->f_next;} else{ while(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); advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 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 = fh[i]; 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; } advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts 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 = fh[i]; 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 tids[THREAD_NUM]; void* tret = NULL; i = j = fid = err = 0; printf("main start\n"); for (i = 0; i THREAD_NUM; i++ ) { err = my_pthread_create(&tids[i], i); if (0 != err) { printf("pthread_create failed\n"); return -1; } } for ( i = 0; i < THREAD_NUM; i++ ) { err = pthread_join(tids[i], &tret); if (0 != err) { printf("pthread_join failed\n"); return -1; } } struct foo *fp = NULL; int cnt = 0; for (i = 0; i f_id, fp->f_count); for ( j = 0; j < THREAD_NUM; j++) { printf("M ", fp->visited[j]); } printf("\n"); fp = fp->f_next; cnt++; } } printf("there are %d foos totally\n", cnt); printf("main end\n"); return 0; } advantages, facing the Yangtze River Delta, Pearl River Delta, Shandong peninsula, Beijing-Tianjin-Tangshan area, targeted investment promotion several times. Industry-led unit and various town streets are developing detailed promotion plans, the basis of what is specific to each industry, has formed a kind of scale, need to bring in the kind of projects each town street has the kind of resources, what location, focus on the development of the kind of industries. Four stroke technology and science. Determinants of technology is the County's economic development through scientific and technological innovation, improve product value-added and improve economic competitiveness, is the inevitable way to achieve leapfrog development. Talent is the city center city's first resource, is the primary industry development, and revitalization of the social protection. So, in terms of skills: to catch ... Competitive. Encouraging enterprises to actively undertake science and technology projects, strengthening research cooperation, using advanced technology to increase the added value of their products to ensure that introduction of transforming scientific and technological achievements during the year ... ... New research enterprises ... ... Home, creating innovative platforms ... ... Generic classifier. In: first, we must strengthen the cultivation of talent, efforts to increase vocational education and training and meet the basic demand of enterprise development. Then introduced a number of the county needed scarce education, health professionals, meet social development needs to invest more in machinery, pharmaceutical manufacturing research and development in higher education, such as the introduction of talent to meet the need. Third, we must rely on ... ... Post-doctoral workstation, ... ... Food research station platform, to the XX University to find experts
本文档为【使用二个互斥量的多线程程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_721103
暂无简介~
格式:doc
大小:47KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-11-10
浏览量:11