Interlocked.Exchange(Object, Object), Option Strict On, and Thread Safety

  • Thread starter Eduardo Garcia-Prieto
  • Start date
E

Eduardo Garcia-Prieto

I have come accross a problem in using the
Interlocked.Exchange(Object, Object) method while using Option Strict
On in my project. I have a private class structure variable which can
be updated by a separate thread. In order to guarantee that the
variable can be updated in an atomic operation it occurred to me that
I could use Interlocked.Exchange.

Class MyClass

Private Structure MyStructure
Dim Value As String
End Structure

Private _myStruct As MyStructure

Public ReadOnly Property Value As String
Get
Return _myStruct.Value
End Get
End Property

'Method that can be called by another thread
Public Sub UpdateMyStruct()
Dim NewStruct As MyStructure
NewStruct.Value = "B"
Interlocked.Exchange(_myStruct, NewStruct)
End Sub

End MyClass


The problem is that when Option Strict is On, the compiler issues a
build error on the line with the call to
Interlocked.Exchange(_myStruct, NewStruct):
....
Overload resolution failed because no accessible 'Exchange' can be
called with these arguments:
'Public Shared Function Exchange(ByRef location1 As Object, value
As Object) As Object':
Option Strict On disallows implicit conversions from 'System.Object'
to 'MyNamespace.MyClass.MyStructure'.
....

To work around it I have implemented a ReaderWriterLock in the class
to synchronise access to the private structure variable.

Has anyone dealt with this situation in a better way? I don't want to
turn Option Strict off.
 
G

Guest

Unless I am mistaken, this is because your struct is by nature a value type, where as an object is a reference type. Try chaning your struct to a class and see if this helps

Da

----- Eduardo Garcia-Prieto wrote: ----

I have come accross a problem in using th
Interlocked.Exchange(Object, Object) method while using Option Stric
On in my project. I have a private class structure variable which ca
be updated by a separate thread. In order to guarantee that th
variable can be updated in an atomic operation it occurred to me tha
I could use Interlocked.Exchange

Class MyClas

Private Structure MyStructur
Dim Value As Strin
End Structur

Private _myStruct As MyStructur

Public ReadOnly Property Value As Strin
Ge
Return _myStruct.Valu
End Ge
End Propert

'Method that can be called by another threa
Public Sub UpdateMyStruct(
Dim NewStruct As MyStructur
NewStruct.Value = "B
Interlocked.Exchange(_myStruct, NewStruct
End Su

End MyClas


The problem is that when Option Strict is On, the compiler issues
build error on the line with the call t
Interlocked.Exchange(_myStruct, NewStruct)
...
Overload resolution failed because no accessible 'Exchange' can b
called with these arguments
'Public Shared Function Exchange(ByRef location1 As Object, valu
As Object) As Object'
Option Strict On disallows implicit conversions from 'System.Object
to 'MyNamespace.MyClass.MyStructure'
...

To work around it I have implemented a ReaderWriterLock in the clas
to synchronise access to the private structure variable

Has anyone dealt with this situation in a better way? I don't want t
turn Option Strict off
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top