Question: Write a multithreaded pi estimator. Create 2 0 threads. Have each thread generate 1 0 0 0 0 ( x , y ) pairs. Use

Write a multithreaded pi estimator. Create 20 threads. Have each thread generate 10000(x,y) pairs. Use srand48_r to seed the random number generator with the thread's ID (your 20 threads should be numbered 0-19, with each thread's id value passed in during the call to pthread_create). Use drand48_r to generate random floating-point numbers between 0 and 1. Do not strictly follow the website example because your answer will be "wrong"; also srand() and rand() aren't thread-safe which is why we cannot rely on them here.
struct drand48_data randbuf;
srand48_r(threadid, &randbuf);
double randx, randy;
for(int i =0; i <10000; i++){
drand48_r(&randbuf, &randx);
drand48_r(&randbuf, &randy);
//you can calculate and record circle/square points here
}
Use randx and randy to determine whether the (randx, randy) coordinate is inside of the unit circle or not. All threads should contribute to this count. Make sure you eliminate the race condition by protecting the shared variables with a mutex. Make sure you wait for each of the 20 threads to complete all of its 10,000 iterations. Calculate the estimate for pi when all threads are done.
pi =4*(double)(circle_points)/ square_points;
Finally, print the estimated value with printf("%.7g
",...) and submit the estimate by pasting

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!