By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,729 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,729 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.