user394307
user394307

Reputation:

Does return inside a lock makes any difference?

Sample One

Public _objLock As Object = New Object
Public ReadOnly Property MyObjects() As IEnumerable(Of Object)
    Get
        SyncLock _objLock
            If _myObjects Is Nothing Then
                _myObject = LoadMyObjects()
            End If
            Return _myObjects
        End SyncLock
    End Get
End Property

Sample Two

Public _objLock As Object = New Object
Public ReadOnly Property MyObjects() As IEnumerable(Of Object)
    Get
        SyncLock _objLock
            If _myObjects Is Nothing Then
                _myObject = LoadMyObjects()
            End If              
        End SyncLock
        Return _myObjects
    End Get
End Property

Will there be any difference between these implementations ?

Upvotes: 9

Views: 1221

Answers (1)

kemiller2002
kemiller2002

Reputation: 115528

No, returning inside a lock makes no difference. Once you leave the lock, it will cease to exist.

Upvotes: 8

Related Questions