hi i'm currently facing race condition in variable, i don't know how to solve this event..
particulary in race condition,
i have code that cut and replace if the letters has been found
example i have a word "cat", if i found the letter "a" it will be removed to the word "cat"
so the remaining would be "ct", repeat the process until it will completely done..
so i have created it and already fully functioning in 1 thread..
but when i transfer it on a parallel.for loop, it acts weird, i dig in deeper and i found out that the process is to fast
and it overwrites the variable, so i try the synclock method, but it slows the process.. and i try the interlock but no good it acts weird because it doesnt process the ordering like "1.2.3.4.5", what i want to do is to preserve the speed without using synclock..
so here's the code.
and this is the part where the race condition occur, it produces error that it should be equal to zero or greater than zero, because the variable is less than zero or null ( i guess )
i wonder how to implement the Interlocked method here??? i think of it as a preliminary solution, any suggestion/comments guys, any related topic on parallel.for loop is a great big help.. thank you in advance.
particulary in race condition,
i have code that cut and replace if the letters has been found
example i have a word "cat", if i found the letter "a" it will be removed to the word "cat"
so the remaining would be "ct", repeat the process until it will completely done..
so i have created it and already fully functioning in 1 thread..
but when i transfer it on a parallel.for loop, it acts weird, i dig in deeper and i found out that the process is to fast
and it overwrites the variable, so i try the synclock method, but it slows the process.. and i try the interlock but no good it acts weird because it doesnt process the ordering like "1.2.3.4.5", what i want to do is to preserve the speed without using synclock..
so here's the code.
Code:
sqrt = Math.Ceiling(Math.Sqrt(13599))
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)
and this is the part where the race condition occur, it produces error that it should be equal to zero or greater than zero, because the variable is less than zero or null ( i guess )
Code:
tmpChar = Mid(tmpChar, 1, InStr(tmpChar, numToletter(xmod(z))) - 1) & Mid(tmpChar, InStr(tmpChar, numToletter(xmod(z))) + 1)
i wonder how to implement the Interlocked method here??? i think of it as a preliminary solution, any suggestion/comments guys, any related topic on parallel.for loop is a great big help.. thank you in advance.