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