Question Is there a way to avoid race condition ( Parallel ) like this??

tang3li2

Member
Joined
Mar 2, 2011
Messages
8
Programming Experience
Beginner
Hi, i'm trying to create the following code..

VB.NET:
        sqrt = Math.Ceiling(Math.Sqrt(2549)) 
        a = 0
        xchar2 = ""


        Parallel.For(1, sqrt + 1, Sub(o)                                 
                                 Dim a, b, c, i, y, z As Long
                                 i = 2
                                 a = 0 
                                 If o = 1 Then
                                     b = 1
                                     c = sqrt
                                 Else
                                     b = ((o * sqrt ) + 1) - sqrt 
                                     c = (o * (sqrt ))
                                 End If
                                 For y = b To c
                                     a = y
                                     tmpChar = "cat"
                                     For z = 0 To i
                                         xmod(z) = a Mod 26
                                         a = ((a - a Mod 26) / 26)
                                         If InStr(tmpChar, numToletter(xmod(z))) = 0 Then
                                             bListed1 = False
                                             Exit For
                                         Else
                                             tmpChar = Mid(tmpChar, 1, InStr(tmpChar, numToletter(xmod(z))) - 1) & Mid(tmpChar, InStr(tmpChar, numToletter(xmod(z))) + 1)
                                             If z = 0 Then
                                                 If check2(numToletter(xmod(z))) Then
                                                     xchar2 = numToletter(xmod(z))
                                                     bListed1 = True
                                                 Else
                                                     xchar2 = ""
                                                     bListed1 = False
                                                     Exit For
                                                 End If
                                             Else
                                                 If check2(numToletter(xmod(z))) Then
                                                     xchar2 &= numToletter(xmod(z))
                                                     bListed1 = True
                                                 Else
                                                     xchar2 = ""
                                                     bListed1 = False
                                                     Exit For
                                                 End If
                                             End If
                                         End If
                                         '---
                                     Next
                                     If bListed1 Then
                                         Debug.Print(StrReverse(xchar2))
                                     End If
                                 Next
                             End Sub)


The above code, raise a race condition, it overwrites the previous data ( E.G "xmod(z), tmpChar, a,blisted1,xchar2" ) that the previous threads needed, i understand i need to use SYNCLOCK, but it to much to use it, it reduce the speed of the parallel..

Is there a way to enhance this without using the SYNCLOCK??
 

Latest posts

Back
Top