B
Bob Day
Ok, I have done a lot of reading(of the newsgroup answers, help files and
MSDN articles) of synclock.
I understand what you are saying in the newsgroup, and it is very helpful.
It does, however, directly contradict what the VS 2003 help file says, and
it seems that various posters to my original questions on Synclock disagree
with each other. It seems, that the best practice is to use sysnlock to
protect code, not variables. That generates further questions.
Consider: Sub1 locks a section of code, not the global_variable (this is, I
think, the best practice on how it should be done). Sub 2 locks the global
variable. Now consider sub1A. If Sub1 and Sub1A are called by different
threads, it would seem to be that the value of Global_Varialbe would be
unpredictable, because while the code is locked in each sub, the varialbe it
not, so two different threads could change it at the exact same moment. Now
conisder 2B, since the variablle is locked, the results would be
predictable. Two threads could not modify the Global_Varialbe at the same
time (one would proceed the other).
Your thoughts? Thanks!
Bob Day
Private Sub1
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1
Private Sub1A
' different code, but ultimatly also modifys Global_Variable
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1A
Private Sub2
' lock the variable itself
Synclock(Global_Variable)
Global_Variable =Local_Variable + 1
End Synclock
End sub2
Private Sub2A
' different code, but ultimatly also modifys Global_Variable
' lock the variable
Synclock(Global_Variable)
Global_Variable =Local_Varialbe + 1
End Synclock
End sub2A
MSDN articles) of synclock.
I understand what you are saying in the newsgroup, and it is very helpful.
It does, however, directly contradict what the VS 2003 help file says, and
it seems that various posters to my original questions on Synclock disagree
with each other. It seems, that the best practice is to use sysnlock to
protect code, not variables. That generates further questions.
Consider: Sub1 locks a section of code, not the global_variable (this is, I
think, the best practice on how it should be done). Sub 2 locks the global
variable. Now consider sub1A. If Sub1 and Sub1A are called by different
threads, it would seem to be that the value of Global_Varialbe would be
unpredictable, because while the code is locked in each sub, the varialbe it
not, so two different threads could change it at the exact same moment. Now
conisder 2B, since the variablle is locked, the results would be
predictable. Two threads could not modify the Global_Varialbe at the same
time (one would proceed the other).
Your thoughts? Thanks!
Bob Day
Private Sub1
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1
Private Sub1A
' different code, but ultimatly also modifys Global_Variable
' create an arbitrary variable to lock a section of code
Dim Lock1 as new object
Synclock(Lock1)
Global_Varialbe = Local_Variable + 1
End Synclock
end sub1A
Private Sub2
' lock the variable itself
Synclock(Global_Variable)
Global_Variable =Local_Variable + 1
End Synclock
End sub2
Private Sub2A
' different code, but ultimatly also modifys Global_Variable
' lock the variable
Synclock(Global_Variable)
Global_Variable =Local_Varialbe + 1
End Synclock
End sub2A