c# - I'm trying to calculate some expression using multithreading. In some threads value iterResult equals 0. Why? -


i'm trying calculate expression using multithreading. each thread uses it's own data. in threads value iterresult equals 0. can't understand why?

01:parameterizedthreadstart expr = threadid => 02:{ 03:    int ithreadid = (int)threadid; 04: 05:    (uint currentn = nstarts[ithreadid] + 1; currentn < nends[ithreadid]; currentn++) 06:    { 07:        uint iterresult = 1; 08:        uint i; 09: 10:        (i = 1; < currentn; i++) 11:           iterresult *= currentn + i; 12: 13:        console.writeline("thread #{0}; currentn = {1}, iterresult = {2}, = {3}", threadid, currentn, iterresult, i); 14: 15:        results[ithreadid] += (1 / (iterresult * 2 * currentn)); 16:    } 17:}; 

i found problem. currentn takes values 1 93 , i 1 73. so, after few iteration of iterresult *= currentn + i; iterresult becomes large. in case becomes negative or equal 0. corrected cycle for:

for (k = 1; k < currentn - nstarts[ithreadid]; k++) {     if ((iterresult *= currentn + k) == 0)         console.writeline("thread #{0}; currentn = {1}, iterresult = {2}, = {3}", threadid, currentn, iterresult, k); } 

Comments