M
mpaine
Hi everyone,
I wrote some code and it seems ok to me but since it is going into a
high-use production environment, I wanted some peer-review if possible.
Basically, I wanted a way to know if an object is being "reset" and
only allow one thread to reset it at any one time. This is what I came
up with:
Private _lockResetSync As Object = New Object()
Private _lockResetWaitEvent As ManualResetEvent = New
ManualResetEvent(False)
Private _IsResetting As Boolean = False
Public Sub Reset(ByVal cmd As Object)
Dim _ThisThreadIsResetting As Boolean = False
While Not _ThisThreadIsResetting ' pseudo-spin-lock for
thread safety
If _IsResetting Then
_lockResetWaitEvent.WaitOne()
Exit Sub
End If
SyncLock _lockResetSync
If Not _IsResetting Then
_lockResetWaitEvent.Reset()
_IsResetting = True
_ThisThreadIsResetting = True
End If
End SyncLock
End While
Try
' !! Do time-consuming thread-safe stuff here
Finally
_IsResetting = False
_lockResetWaitEvent.Set()
End Try
End Sub
Public ReadOnly Property IsResetting() As Boolean
Get
Return _IsResetting
End Get
End Property
I feel if two threads came into Reset() at the same clock-tick (hehe),
this would still work correctly. Also, external objects would know
what is going on. Will this work and if not, what is a better way to
do this?
Thank you,
Michael
I wrote some code and it seems ok to me but since it is going into a
high-use production environment, I wanted some peer-review if possible.
Basically, I wanted a way to know if an object is being "reset" and
only allow one thread to reset it at any one time. This is what I came
up with:
Private _lockResetSync As Object = New Object()
Private _lockResetWaitEvent As ManualResetEvent = New
ManualResetEvent(False)
Private _IsResetting As Boolean = False
Public Sub Reset(ByVal cmd As Object)
Dim _ThisThreadIsResetting As Boolean = False
While Not _ThisThreadIsResetting ' pseudo-spin-lock for
thread safety
If _IsResetting Then
_lockResetWaitEvent.WaitOne()
Exit Sub
End If
SyncLock _lockResetSync
If Not _IsResetting Then
_lockResetWaitEvent.Reset()
_IsResetting = True
_ThisThreadIsResetting = True
End If
End SyncLock
End While
Try
' !! Do time-consuming thread-safe stuff here
Finally
_IsResetting = False
_lockResetWaitEvent.Set()
End Try
End Sub
Public ReadOnly Property IsResetting() As Boolean
Get
Return _IsResetting
End Get
End Property
I feel if two threads came into Reset() at the same clock-tick (hehe),
this would still work correctly. Also, external objects would know
what is going on. Will this work and if not, what is a better way to
do this?
Thank you,
Michael