Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- pthread_t tid[10000];
- pthread_mutex_t mtx;
- int count=0;
- double r()
- {
- return (double)rand() / (double)RAND_MAX ;
- }
- float distanta(float x,float y,float z,float t)
- {
- return sqrt((x-y)*(x-y)+(z-t)*(z-t));
- }
- void* function(void* arg)
- {
- int nr=*(int*)arg;
- int i;
- pthread_mutex_lock(&mtx);
- //printf("Threadul %d : \n",nr);
- for(i=0;i<10000;i++)
- {
- float random1=r();
- float random2=r();
- float random3=r();
- float random4=r();
- if(distanta(random1,random2,random3,random4)<0.50)
- count++;
- }
- //printf("\n");
- //printf("Count este momentan: %d \n",count);
- pthread_mutex_unlock(&mtx);
- }
- int main()
- {
- srand(time(0));
- pthread_mutex_init(&mtx,NULL);
- int nr[10000];
- int i;
- for(i=0;i<10000;i++)
- nr[i]=i;
- for(i=0;i<10000;i++)
- pthread_create(&tid[i],NULL,function,&nr[i]);
- for(i=0;i<10000;i++)
- pthread_join(tid[i],NULL);
- printf("procentul este: %f \n", (float)100*count/100000000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement