I have a feeling my understanding of locking requirements is a bit off. As I understand it, any property that has a possibility to be used from another thread should be locked on both read and write. So I have lots of code that looks something like this:
Function PropValue() As String
Self.mLock.Enter
Var Value As String = Self.mPropValue
Self.mLock.Leave
Return Value
End Function
Function PropValue(Assigns Value As String)
Self.mLock.Enter
Self.mPropValue = Value
Self.mLock.Leave
End Function
This way, there is absolutely zero opportunity for two threads to access the same property at the same time.
What’s bugging me is the lock itself is stored in a property. Which leads me to suspect that I don’t actually need to be locking properties and that Xojo is doing that for me. It may be that the only time I need to worry about locking is when doing multiple actions with the same property when state is important:
Self.mLock.Enter
If Self.mDict.HasKey("Key") = False Then
Self.mDict.Value("Key") = "Value"
End If
Self.mLock.Leave
And maybe even multiple accessors on the same line?
Self.mLock.Enter
Var Prefix As String = Self.mPropValue.Left(Self.mPropValue.IndexOf(":"))
Self.mLock.Leave
Can anybody confirm which is correct?
6 posts - 4 participants