They are marked thread-safe, only when the *author (in this case MS) has
taken the time to verify they are thread safe (which is easy if they don't
touch shared data). As most statics in the framework (not sure if all are)
are documented with the thread safe attribute, it is easy to assume that is
some kind of special gaureentee all statics give - which is not the case.
You can easily make a non thread safe static method via bug or on purpose.
--
William Stacey [C# MVP]
"Anthony Paul" <an**********@g mail.comwrote in message
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
| Here's the deal... the only difference (that I am aware of) between
| static and instance methods is that instance methods pass the "this"
| pointer silently while static methods do not; that's it. I think that
| the reason why static methods have been marked as "thread safe" is
| that they are assuming that, since the method is static, you won't be
| accessing any shared-state as you most likely would in an instance
| method. Both instance methods and static methods *are* thread-safe as
| long as you don't access any shared-state (eg. instance/static fields)
| since each thread has its own local stack.
|
| In short, if you have a multi-threaded application that needs to
| access shared-state, you need to synchronize all access via a locking
| mechanism such as lock, mutexes, monitor, etc...
|
| Here's a link that might help :
http://www.odetocode.com/Articles/313.aspx
|
| Regards,
|
| Anthony
|
| On Mar 20, 4:08 am, "Laser Lu" <laser...@163.c omwrote:
| I was often noted by Thread Safety declarations when I was reading .NET
Framework Class Library documents in MSDN.
| The declaration is usually described as 'Any public static (Shared in
Visual Basic) members of this type are thread safe. Any instance members are
not guaranteed to be thread safe.'
| So, does this mean All the static/shared methods written in .NET
compatible programming language, such as C#, VB.NET, are guaranteed to be
synchronized and thread safe? Or else, we still need to implement our custom
code to ensure the thread-safty for static methods?
|
|