A local variable is considered threadsafe if it is declared in the prodecure
that is started on the thread, but it doesn't seem to be the case if the
variable is a class. Is this correct behavior? If so, why?
Sub whyIsThis()
dim mClass as new myClass
mClass.myInteger = 3
ThreadPool.QueueUserWorkItem(AddressOf SafeForThreading,5)
ThreadPool.QueueUserWorkItem(AddressOf UnsafeforThreading,mClass)
ThreadPool.QueueUserWorkItem(AddressOf
ThisIsAlsoUnsafeforThreading,mClass)
End sub
Sub SafeForThreading(state as object)
dim X as integer = state
x+=1
console.writeline x
end Sub
Sub UnsafeforThreading(state as object)
dim X as myClass = ctype(state,myClass)
myclass.myInteger +=1
console.writeline X.myInteger
End Sub
Sub ThisIsAlsoUnsafeforThreading(state as object)
dim X as New myClass
X = ctype(state,myClass)
myclass.myInteger +=1
console.writeline X.myInteger
End Sub
Class myClass
Public myInteger as Integer
End Class 3 1001
Hi ZorpiedoMan,
I think this is caused by the difference between Value type and Reference
Type.
Since Value type will will pass value and Reference Type will pass
reference(i.e. point)
In the below unsafe case all the thread are holding a reference to one
object.
But the other case, the 5 is a value type, although it will be box in a
Object(reference type),
when you set it to a Interger, the reference type will be unbox to a value
type. Sub whyIsThis() dim mClass as new myClass mClass.myInteger = 3
ThreadPool.QueueUserWorkItem(AddressOf SafeForThreading,5) ThreadPool.QueueUserWorkItem(AddressOf UnsafeforThreading,mClass) ThreadPool.QueueUserWorkItem(AddressOf ThisIsAlsoUnsafeforThreading,mClass) End sub
Sub SafeForThreading(state as object)
(when pass into the thread, the 5 was box into state object
dim X as integer = state
the 5 in the object state will be unbox to a integer X, and it is a value
type. x+=1 console.writeline x end Sub
The other two case is always pass as reference, they are all reference
type. No boxing or unboxing occur. Sub UnsafeforThreading(state as object) dim X as myClass = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
Sub ThisIsAlsoUnsafeforThreading(state as object) dim X as New myClass X = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
For detailed informaiton, you may read "Applied Microsoft? .NET Framework
Programming" written by Jeffrey Richter (Wintellect). http://www.microsoft.com/MSPress/books/5353.asp
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
thanks. It's starting to get through my thick skull little by little. I
put the Chat session noted in the next thread on my calendar.
-js
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:IY**************@cpmsftngxa07.phx.gbl... Hi ZorpiedoMan,
I think this is caused by the difference between Value type and Reference Type. Since Value type will will pass value and Reference Type will pass reference(i.e. point) In the below unsafe case all the thread are holding a reference to one object. But the other case, the 5 is a value type, although it will be box in a Object(reference type), when you set it to a Interger, the reference type will be unbox to a value type.
Sub whyIsThis() dim mClass as new myClass mClass.myInteger = 3
ThreadPool.QueueUserWorkItem(AddressOf SafeForThreading,5) ThreadPool.QueueUserWorkItem(AddressOf UnsafeforThreading,mClass) ThreadPool.QueueUserWorkItem(AddressOf ThisIsAlsoUnsafeforThreading,mClass) End sub
Sub SafeForThreading(state as object) (when pass into the thread, the 5 was box into state object
dim X as integer = state
the 5 in the object state will be unbox to a integer X, and it is a value type. x+=1 console.writeline x end Sub
The other two case is always pass as reference, they are all reference type. No boxing or unboxing occur. Sub UnsafeforThreading(state as object) dim X as myClass = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
Sub ThisIsAlsoUnsafeforThreading(state as object) dim X as New myClass X = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
For detailed informaiton, you may read "Applied Microsoft? .NET Framework Programming" written by Jeffrey Richter (Wintellect). http://www.microsoft.com/MSPress/books/5353.asp
Regards, Peter Huang Microsoft Online Partner Support Get Secure! www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
ZorpiedoMan, that is started on the thread, but it doesn't seem to be the case if the variable is a class. Is this correct behavior? If so, why?
Yes that is correct, as it is the way you pass & share information between
threads.
As Peter stated, a class creates a reference type, so you can have multiple
variables referring to the same instance of the class on the heap.
If you are going to have multiple threads accessing the same object, then
you need to learn to use & learn when to use the following (this is not an
all inclusive list, but it is a good starting point):
- SyncLock statement (wrapper for System.Threading.Monitor)
- System.Threading.Interlocked
- System.Threading.AutoResetEvent
- System.Threading.ManualResetEvent
- System.Threading.Mutex
- System.Threading.ReaderWriterLock
Hope this helps
Jay
"ZorpiedoMan" <js******@dscicorp.com> wrote in message
news:ee**************@TK2MSFTNGP10.phx.gbl... A local variable is considered threadsafe if it is declared in the
prodecure that is started on the thread, but it doesn't seem to be the case if the variable is a class. Is this correct behavior? If so, why?
Sub whyIsThis() dim mClass as new myClass mClass.myInteger = 3
ThreadPool.QueueUserWorkItem(AddressOf SafeForThreading,5) ThreadPool.QueueUserWorkItem(AddressOf UnsafeforThreading,mClass) ThreadPool.QueueUserWorkItem(AddressOf ThisIsAlsoUnsafeforThreading,mClass) End sub
Sub SafeForThreading(state as object) dim X as integer = state x+=1 console.writeline x end Sub
Sub UnsafeforThreading(state as object) dim X as myClass = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
Sub ThisIsAlsoUnsafeforThreading(state as object) dim X as New myClass X = ctype(state,myClass) myclass.myInteger +=1 console.writeline X.myInteger End Sub
Class myClass Public myInteger as Integer End Class This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Frank |
last post by:
Hi,
we are using oracle clients (Release 9.0.1.0.1 - Production)
on an NT4 (Service Pack6) computers. the server is a W2K,
(Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
With the...
|
by: Marc Champagne |
last post by:
Hi folks!
I have built a VB project in VS.NET 7 which also includes a
deployment project.
Everything builds ok.
When I install the setup package, it installs without a
glitch.
|
by: Marc Champagne |
last post by:
Hi folks!
I have built a VB project in VS.NET 7 which also includes a
deployment project.
Everything builds ok.
When I install the setup package, it installs without a
glitch.
|
by: Ray |
last post by:
Hello,
Greetings! I'm looking for a solid C++ multithreading book. Can you
recommend one? I don't think I've seen a multithreading C++ book that
everybody thinks is good (like Effective C++ or...
|
by: Frank |
last post by:
Hi,
we are using oracle clients (Release 9.0.1.0.1 - Production)
on an NT4 (Service Pack6) computers. the server is a W2K,
(Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
With the...
| |
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,...
|
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...
|
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,...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |