linux-C多线程读写互斥_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > linux-C多线程读写互斥

linux-C多线程读写互斥

 2010/9/19 23:30:39  deepfuture  http://deepfuture.javaeye.com  我要评论(0)
  • 摘要:计算1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......其中1/n由n个线程计算并使用写互斥写入中间数组,2个线程用读互斥从中间数组中读取结果分别进行1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......的计算。deepfuture@deepfuture-laptop:~/private/mytest$gcc-lpthread-std=gnu99-otest2test2.ctest2.c:Infunction‘myprint1’:test2
  • 标签:linux C多线程读写互斥

?

计算1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......

其中1/n由n个线程计算并使用写互斥写入中间数组,2个线程用读互斥从中间数组中读取结果分别进行1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......的计算。

deepfuture@deepfuture-laptop:~/private/mytest$ gcc -lpthread -std=gnu99 -o test2 test2.c

test2.c: In function ‘myprint1’:

test2.c:32: warning: implicit declaration of function ‘sleep’

deepfuture@deepfuture-laptop:~/private/mytest$ ./test2

please input an integer:(<=1000)2

1/1 finished,push 1.0000000000

*******1/1:1.0000000000 computed result 1.0000000000

1/1:1.0000000000 added result 1.0000000000

1/2 finished,push 0.5000000000

1/2:0.5000000000 added result 1.5000000000

================add finish!============ result:1.5000000000

*******1/2:0.5000000000 computed result 0.5000000000

================compute finish!============ result:0.5000000000

deepfuture@deepfuture-laptop:~/private/mytest$ ./test2

please input an integer:(<=1000)3

1/1 finished,push 1.0000000000

1/1:1.0000000000 added result 1.0000000000

*******1/1:1.0000000000 computed result 1.0000000000

1/2 finished,push 0.5000000000

1/2:0.5000000000 added result 1.5000000000

*******1/2:0.5000000000 computed result 0.5000000000

1/3 finished,push 0.3333333333

*******1/3:0.3333333333 computed result 0.8333333333

================compute finish!============ result:0.8333333333

1/3:0.3333333333 added result 1.8333333333

================add finish!============ result:1.8333333333

deepfuture@deepfuture-laptop:~/private/mytest$ ./test2

please input an integer:(<=1000)4

1/1 finished,push 1.0000000000

1/1:1.0000000000 added result 1.0000000000

*******1/1:1.0000000000 computed result 1.0000000000

*******1/2:0.5000000000 computed result 0.5000000000

1/2 finished,push 0.5000000000

1/2:0.5000000000 added result 1.5000000000

1/3 finished,push 0.3333333333

*******1/3:0.3333333333 computed result 0.8333333333

1/3:0.3333333333 added result 1.8333333333

1/4 finished,push 0.2500000000

1/4:0.2500000000 added result 2.0833333333

================add finish!============ result:2.0833333333

*******1/4:0.2500000000 computed result 0.5833333333

================compute finish!============ result:0.5833333333

deepfuture@deepfuture-laptop:~/private/mytest$?

#include <pthread.h>
#include <bits/pthreadtypes.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXS 1000 



double myjg[MAXS+1];//计算结果存放位置
int max;
pthread_rwlock_t myrwlock; 
pthread_rwlockattr_t myrwlockattr; 
pthread_t threads[MAXS+2]; 
 

void *mycomp(void *x){//计算1/i的结果,计算结果放在一个数组中。
int i;
pthread_rwlock_wrlock(&myrwlock); //互斥写锁,没有线程拥有互斥读锁时,可以加互斥写锁,临界区,完成对i的累加,保证不被多个线程同时修改i
myjg[0]++;
i=myjg[0];//myjg[0]存放着线程已经计算到的i。
pthread_rwlock_unlock(&myrwlock); 
myjg[i]=(1/(double)i);
printf("1/%d finished,push %.10f\n",i,myjg[i]); 
}

void *myprint1(void *xx){//读取数组,将计算结果累加,最终完成1/1+1/2+1/3+......+1/n的计算
int maxi;
int curi=1;
double jg=0;
  while(curi<=max)
  {
	  sleep(1);
	  pthread_rwlock_rdlock(&myrwlock); //互斥读锁,在互斥写解锁后可以有多个互斥读锁同时存在,临界区,取出正确的i,保证此时没有线程写i
	  maxi=myjg[0];
      pthread_rwlock_unlock(&myrwlock);
      
            for (;curi<=maxi;curi++){
  		        jg+=myjg[curi];
                printf("1/%d:%.10f added result %.10f\n",curi,myjg[curi],jg);   		        
  		    } 
  }
    	   printf("================add finish!============ result:%.10f\n",jg);//输出累加结果。
}

void *myprint2(void *xx){//读取数组,将计算结果完成1/1+1/2-1/3+1/4-1/5......的计算
int maxi;
int curi=1;
int fh=1;
double jg=0;
  while(curi<=max)
  {
	  sleep(1);
	  pthread_rwlock_rdlock(&myrwlock); //互斥读锁,在互斥写解锁后可以有多个互斥读锁同时存在,临界区,取出正确的i,保证此时没有线程写i
	  maxi=myjg[0];
      pthread_rwlock_unlock(&myrwlock);
 
            for (;curi<=maxi;curi++){                
  		        jg+=fh*myjg[curi];
                printf("*******1/%d:%.10f computed result %.10f\n",curi,myjg[curi],jg);  
                fh=-fh;   		        
  		    }	  
  }
  		 printf("================compute finish!============ result:%.10f\n",jg);//输出累加结果  
  pthread_rwlock_destroy(&myrwlock);  
  pthread_rwlockattr_destroy(&myrwlockattr);    
}

int main(){
//计算1+1/2+1/3+......和1+1/2-1/3+1/4-1/5......
 

  pthread_rwlockattr_init(&myrwlockattr);
  pthread_rwlockattr_setpshared(&myrwlockattr,PTHREAD_PROCESS_PRIVATE);//设置互斥为同一进程共享,也可设置为多个进程共享
  pthread_rwlock_init(&myrwlock,&myrwlockattr);  

  printf("please input an integer:(<=%d)",MAXS);
  while (scanf("%d",&max),max>MAXS){//n的最大值
      printf("please input an integer:(<=%d)",MAXS);
  };
  //深未来技术http://deepfuture.javaeye.com/
  myjg[0]=0;
  pthread_create(&(threads[0]),NULL,myprint1,NULL);    
  pthread_create(&(threads[1]),NULL,myprint2,NULL);   
  for (int i=2;i<=max+1;i++){
    pthread_create(&(threads[i]),NULL,mycomp,NULL);
      	  sleep(1);
 
  } 
	  sleep(1);
  //for (int i=0;i<=max+1;i++){
  //  pthread_join(threads[i],NULL);  	
 // }  

  return(0);
} 
     
?
发表评论
用户名: 匿名