Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <math.h>
- #include <pthread.h>
- const int NUM_THREADS = 4;
- clock_t start, end;
- static int count = 0;
- void* circle_cnt(void* loopTimes)
- { int times = 0;
- int loop = (int)loopTimes;
- for(int i=0;i<loop;i++)
- {
- double x=(double)rand()/((double)RAND_MAX);
- double y=(double)rand()/((double)RAND_MAX);
- double distance= 1.0*x*x+1.0*y*y;
- if(distance<=1) times++;
- }
- count = count + times;
- pthread_exit(NULL);
- }
- int main(int argc, char*argv[])
- {
- pthread_t tid[NUM_THREADS];
- int n=atoi(argv[1]);
- int div = n/NUM_THREADS;
- double pi = 1.0;
- srand(time(NULL));
- start = clock();
- int i = 0;
- for (i = 0; i < NUM_THREADS; ++i)
- {
- //pthread_attr_t attr;
- //pthread_attr_init(&attr);
- pthread_create(&tid[i], NULL, circle_cnt, (void*) div);
- }
- for (i = 0; i < NUM_THREADS; ++i)
- {
- pthread_join(tid[i], NULL);
- }
- pi=4.0*count/n;
- end = clock();
- double exe_time = ((double)(end - start)) / ( (double)(CLOCKS_PER_SEC) );
- printf("Pi: %lf\n",pi);
- printf("Time: %g seconds", exe_time);
- return 0;
- }
Add Comment
Please, Sign In to add comment