Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <math.h>
- #include <time.h>
- #define INSIDE 1
- #define OUTSIDE 0
- double vzdialenost=0;
- double stredX=1.0;
- double stredY=1.0;
- int countIn=0;
- int countOut=0;
- //pthread_mutex_t mutex;
- int isInCircle(double x, double y){
- vzdialenost=sqrt(pow((stredX-x),2)+pow((stredY-y),2));
- if(vzdialenost<=1)
- return INSIDE;
- else
- return OUTSIDE;
- }
- void* body(void *param){
- int i, countIn1=0;
- int countOut1=0;
- double newX, newY;
- //pthread_mutex_lock(&mutex);
- for(i=0; i<1000; i++){
- newX=(rand()%2000001)/1000000;
- newY=(rand()%2000001)/1000000;
- countOut1++;
- if(isInCircle(newX, newY)==INSIDE)
- countIn1++;
- }
- countOut+=countOut1;
- countIn+=countIn1;
- printf("\nCounters: countOut= %d, countOut1= %d, countIn1= %d, countIn= %d", countOut, countOut1, countIn1, countIn);
- //pthread_mutex_unlock(&mutex);
- return NULL;
- }
- int main(){
- srand(time(NULL));
- //pthread_mutex_init(&mutex, NULL);
- pthread_t* vlakna=(pthread_t*)malloc(sizeof(pthread_t)*20);
- int i;
- for(i=0; i<20; i++){
- pthread_create(&vlakna[i], NULL, body, NULL);}
- for(i=0; i<20; i++){
- pthread_join(vlakna[i], NULL);
- }
- printf("\nPi je %f ",(double)4*countIn/countOut);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement