Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. double calcPiCpp11(const int numThreads, const long numSteps)
  2. {
  3.     double step = 1.0 / (double)numSteps;
  4.     double x, pi;
  5.  
  6.     double* sums = new double[numThreads];
  7.     for (int i = 0; i < numThreads; ++i)
  8.     {
  9.         sums[i] = 0.0f;
  10.     }
  11.  
  12.     const int threadNumSteps = numSteps / numThreads;
  13.     std::vector<std::thread> threads;
  14.  
  15.     for (int i = 0; i < numThreads; ++i)
  16.     {
  17.         int iStart = threadNumSteps * i + 1;
  18.         threads.push_back(std::thread(calcPI, sums, step, threadNumSteps, iStart, i));
  19.     }
  20.  
  21.     for (auto& thread : threads)
  22.     {
  23.         thread.join();
  24.     }
  25.  
  26.     pi = step * std::accumulate(sums, sums + numThreads, 0.0);
  27.  
  28.     delete[] sums;
  29.     return pi;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement