Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Thread safety: is my thread safe?
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Thread safety: is my thread safe?
Miscellaneous
Thread ID:
00961688
Message ID:
00961688
Views:
37
Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

I have seen this for several classes in the help documentation, but what does it acctually mean. Why is it so important to use static members in a muti-threaded environment?

The class I am currently working on connects for a device given an IP and a port, if connect is sucessful it starts a receiving thread that will receive data from the device it is connected to.
Currently the receive method is a static method (because of the thread safety issue) and the receive method will update a static class property with the amount of data read.
The problem I noticed was that the value of the static property was retained after the class object went out of scope and the next time I created the class object, the value was what it was the previous time I received data.

So one solution I had was to set the static property to zero in the class constructor, and this worked just fine, but it seems more like a hack that a well designed class.

The other thing I was thinking about doing was to remove all the static key words and make every property and method instance members, but I am a little affraid of the thread safety issue.

Any comments or input would be appreciated.

Thanks,
Einar
Semper ubi sub ubi.
Next
Reply
Map
View

Click here to load this message in the networking platform