#include #include #include #include #include using namespace std; const int THREAD_COUNT{10}; const int COUNT_LIMIT{200'000'000}; const int KILL_LOOP{1}; void ThreadFunction (int id, double address[], int nThread){ int i; float signTerm = 1; for(int j =0; j < KILL_LOOP; ++j) { address[id-1] = 0; for(i = id * 2 + 1; i < COUNT_LIMIT; i += 2*nThread) { if (i%4 ==1) { signTerm = 1; } else { signTerm = -1; } address[id-1] += signTerm *4.0/static_cast(i); } } return ; } int main() { vector threads; double * storage = new double [THREAD_COUNT]; for(int i = 0; i < THREAD_COUNT; ++i) { thread t(ThreadFunction, i+1, storage, THREAD_COUNT); threads.push_back(move(t)); } double pi{4}; for( int i = 0; i < THREAD_COUNT; ++i) { threads[i].join(); pi += storage[i]; } cout << setprecision(10); cout << "Pi is approximately " << pi << endl; delete[] storage; return 0; }