473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 2390
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lindstrom Greg - glinds | last post by:
I'm using python 2.3 and Windows 2000 "Professional" to access a Microsoft Exchange Server to monitor messages and perform various tasks around the office. My little program is attracted quite a...
1
by: n_o_s_p_a__m | last post by:
Hi, just a quick question: I have 2 objects running in their own threads (let's call them a & b). They both hold a reference to a common object running in a third thread (call it c) which has...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
19
by: steve | last post by:
// What I want to do Use enumerated types with the Interlocked.Exchange methods Suggestions please // My estimation of problem Seems like Interlocked.Exchange only works with ints,...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
15
by: Ryan Liu | last post by:
Hi, Is there any known bug related to Interlocked.Increment(ref var)? My client report var's value going up and down in the client/server multile-thread application. There are about 80...
4
by: rosco | last post by:
I have a multi threading application where multiple threads can read and write to the property. I try to avoid the lock statement on properties that hold none business critical data. I could...
3
by: mukesh78ae | last post by:
Dear All, With the code below I am able to successfully pull out e-mails by using the sql query defined with the webdav's search method. However, I am very unclear how to pull the attachments from...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.