What does it really mean to not be Thread Safe; will some other thread modify the contents of the collection by mistake?ASP.NEt is multithreaded environment. In fact each user session uses its own thread. Something that is accessible to multiply threads (like static fields, application object items etc.) could be accessed simultaneously from different threads. Classes, which designed to allow this kind of access and still behave correctly, are called Thread Safe.
So, why would someone recommend that i not use an object that isn't thread safe with asp.net? Is it because the object was not designed to run in a multi-threaded environment [and may not always work]? Or is it because the object (which is not multi threaded) could reduce the scaleability of my code?
It can lock; it can crash or behave unexpected.
No no no no. The person who said that does not know what he's talking about.
Non thread safe objects should not be exchanged between threads without some precautions. But as long as you stay in one thread, you're perfectly safe to use them.
For example, from within a page request, it's perfectly ok. It starts to be dangerous if you put such an object in Session or Application where it can be accessed by several threads simultaneously.
Hi Bertrand,
Even within separate thread using such an object could be dangerous. For example if it make use of internal static resources (some kind of singleton pattern).
Tuesday, March 13, 2012
Thread Safety and ASP.NET
I use theHybrid Dictionary Object often and was advised not to use it in ASP.NET because it is not Thread Safe.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment