More Multithreading Blues...

Z

ZorpiedoMan

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
 
P

Peter Huang

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.
 
Z

ZorpiedoMan

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

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