/* This are the calculation threads. * they pick the next available position on the memsave array (so they know which 8 digits of pi * they should calculate) and increment the position counter for the others. * WHen calculated a set of digits they save it to the picked position in memory and go on * with the first step. * But they also look every calculation loop if the user has pressed "Stop/Close" (isRunning = false) * if so they terminate themself. */ UINT CCalculate::Calc_Threads(LPVOID pParam) { //CSingleLock id_lock(&cs_id_lock); CSingleLock pos_lock(&cs_pos_lock); //int my_id; //id_lock.Lock(); //if(id_lock.IsLocked()) //pick an number (id) and increment for other threads //{ // my_id = myThreadId; // myThreadId++; // id_lock.Unlock(); //} int *my_id = reinterpret_cast<int*>(pParam); int save_pos = 0, id = 0; double pi, s1, s2, s3, s4; //actual pi digit and the 4 series do { pos_lock.Lock(); if(pos_lock.IsLocked()) { save_pos = position; position++; pos_lock.Unlock(); } id = save_pos * TRUSTED; s1 = series (1, id); s2 = series (4, id); s3 = series (5, id); s4 = series (6, id); pi = 4. * s1 - 2. * s2 - s3 - s4; pi = pi - (int) pi + 1.; ihex (pi, save_pos); if(!isRunning) //user pressed stop, so stop operation NOW return 0; }while(((save_pos + num_cpu) * TRUSTED) < (pi_digits)); isFinished[(*my_id)] = true; //signalise to control_thread that you have finished return 0; }
main() { double pid, s1, s2, s3, s4; double series (int m, int n); void ihex (double x, int m, char c[]); int id = 1000000; #define NHX 16 char chx[NHX]; /* id is the digit position. Digits generated follow immediately after id. */ s1 = series (1, id); s2 = series (4, id); s3 = series (5, id); s4 = series (6, id); pid = 4. * s1 - 2. * s2 - s3 - s4; pid = pid - (int) pid + 1.; ihex (pid, NHX, chx); printf (" position = %i\n fraction = %.15f \n hex digits = %10.10s\n", id, pid, chx); }