Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7. pthread_t tid[10000];
  8. pthread_mutex_t mtx;
  9. int count=0;
  10.  
  11. double r()
  12. {
  13.     return (double)rand() / (double)RAND_MAX ;
  14. }
  15.  
  16. float distanta(float x,float y,float z,float t)
  17. {
  18.  
  19.         return sqrt((x-y)*(x-y)+(z-t)*(z-t));
  20. }
  21.  
  22. void* function(void* arg)
  23. {
  24.         int nr=*(int*)arg;
  25.         int i;
  26.         pthread_mutex_lock(&mtx);
  27.         //printf("Threadul %d : \n",nr);
  28.         for(i=0;i<10000;i++)
  29.                 {
  30.                         float random1=r();
  31.                         float random2=r();
  32.                         float random3=r();
  33.                         float random4=r();
  34.                         if(distanta(random1,random2,random3,random4)<0.50)
  35.                                 count++;
  36.                 }
  37.         //printf("\n");
  38.         //printf("Count este momentan: %d \n",count);
  39.         pthread_mutex_unlock(&mtx);
  40.  
  41. }
  42.  
  43. int main()
  44. {
  45.         srand(time(0));
  46.  
  47.         pthread_mutex_init(&mtx,NULL);
  48.         int nr[10000];
  49.         int i;
  50.         for(i=0;i<10000;i++)
  51.                 nr[i]=i;
  52.         for(i=0;i<10000;i++)
  53.                 pthread_create(&tid[i],NULL,function,&nr[i]);
  54.         for(i=0;i<10000;i++)
  55.                 pthread_join(tid[i],NULL);
  56.  
  57.         printf("procentul  este: %f \n", (float)100*count/100000000);
  58.         return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement