473,624 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I have come accross a problem in using the
Interlocked.Exc hange(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.Exc hange.

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.Exc hange(_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.Exc hange(_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.My Class.MyStructu re'.
....

To work around it I have implemented a ReaderWriterLoc k 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 2430
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.Exc hange(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.Exc hange

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.Exc hange(_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.Exc hange(_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.My Class.MyStructu re'
...

To work around it I have implemented a ReaderWriterLoc k 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
4727
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 bit of attention and I have been asked to add some features, but I don't know how to do a couple of things. Can anyone either help me out with the details, or point me to where I can get the information? Here's my task list: 1. Create a...
1
9797
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 a public boolean field called bCool. So: // a & b
14
7438
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: Interlocked.Increment(ref count); The error says that a volatile field cannot be used as ref or out, but if I don't use the volatile field, the value may be cached away in some method that is just reading the field.
19
7210
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, referencable objects, and floats
3
2736
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 manner. But at the same time if i want to use variable that could be changed in another thread i must use volatile (to prevent optimization). But then i can not use Interlocked.Increment. So i do not see any benefits of having ...
12
2036
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 for all Balances to be 0. When using an Integer things work fine. LONGS do not. We are using the Interlocked methods. I believe the Interlocked.Add method is not thread safe when using longs on 32bit systems. We are aware of...
15
4766
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 clients connect to one server though TCP client. But actully I never decrease var.
4
9382
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 create a special '_propertyLock' object for this none critical data but this could lead to more multithreading problems like deadlocks. I am just looking for atomic read a write operation to a string. There is a Read member but only for typeof long.
3
2377
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 an e-mail. I have gone through various tutorials and based on which I have written the code below using the "X-MS-ENUMATTS" method. Also printed the output below. I would appreciate if you help me from the point where I can pull the attachment from...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.