Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #define INSIDE 1
  8. #define OUTSIDE 0
  9.  
  10. double vzdialenost=0;
  11. double stredX=1.0;
  12. double stredY=1.0;
  13. int countIn=0;
  14. int countOut=0;
  15. //pthread_mutex_t mutex;
  16.  
  17. int isInCircle(double x, double y){
  18. vzdialenost=sqrt(pow((stredX-x),2)+pow((stredY-y),2));
  19. if(vzdialenost<=1)
  20. return INSIDE;
  21. else
  22. return OUTSIDE;
  23. }
  24. void* body(void *param){
  25.  
  26.  
  27. int i, countIn1=0;
  28. int countOut1=0;
  29. double newX, newY;
  30. //pthread_mutex_lock(&mutex);
  31. for(i=0; i<1000; i++){
  32. newX=(rand()%2000001)/1000000;
  33. newY=(rand()%2000001)/1000000;
  34. countOut1++;
  35. if(isInCircle(newX, newY)==INSIDE)
  36. countIn1++;
  37. }
  38. countOut+=countOut1;
  39. countIn+=countIn1;
  40. printf("\nCounters: countOut= %d, countOut1= %d, countIn1= %d, countIn= %d", countOut, countOut1, countIn1, countIn);
  41. //pthread_mutex_unlock(&mutex);
  42. return NULL;
  43. }
  44.  
  45. int main(){
  46. srand(time(NULL));
  47. //pthread_mutex_init(&mutex, NULL);
  48. pthread_t* vlakna=(pthread_t*)malloc(sizeof(pthread_t)*20);
  49. int i;
  50. for(i=0; i<20; i++){
  51. pthread_create(&vlakna[i], NULL, body, NULL);}
  52. for(i=0; i<20; i++){
  53. pthread_join(vlakna[i], NULL);
  54. }
  55.  
  56. printf("\nPi je %f ",(double)4*countIn/countOut);
  57.  
  58. return 0;
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement