Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started