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 threads. Have each thread generate xy pairs. Use srandr to seed the random number generator with the thread's ID your threads should be numbered with each thread's id value passed in during the call to pthreadcreate Use drandr to generate random floatingpoint numbers between and Do not strictly follow the website example because your answer will be "wrong"; also srand and rand aren't threadsafe which is why we cannot rely on them here.
struct dranddata randbuf;
srandrthreadid &randbuf;
double randx, randy;
forint i ; i ; i
drandr&randbuf, &randx;
drandr&randbuf, &randy;
you can calculate and record circlesquare 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 threads to complete all of its iterations. Calculate the estimate for pi when all threads are done.
pi doublecirclepoints squarepoints;
Finally, print the estimated value with printfg
and submit the estimate by pasting
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
