Parallel.For Inconsistency results

Posted by ni Gue ??? on Programmers See other posts from Programmers or by ni Gue ???
Published on 2013-11-02T07:37:36Z Indexed on 2013/11/02 10:12 UTC
Read the original article Hit count: 255

I am using VB.net to write a parallel based code. I use Parallel.For to generate pairs of 500 objects or in combination C(500,2) such as the following code; but I found that it didn't always generate all combinations which should be 124750 (shown from variable Counter). No other thread was runing when this code was run. I am using a Win-7 32 Bit desktop with Intel Core i5 CPU [email protected], 3.33 GHz and RAM 2GB. What's wrong with the code and how to solve this problem? Thank You.

    Dim Counter As Integer = 0
    Parallel.For(0, 499, Sub(i)
                     For j As Integer = i + 1 To 499
                         Counter += 1
                         Console.Write(i & ":" & j)
                     Next
                 End Sub)
    Console.Writeline("Iteration number: " & Counter)

© Programmers or respective owner

Related posts about vb.net

Related posts about parallel-programming