In .NET how can I know if a class or method is thread safe or not? Is it by default not thread safe?
views:
194answers:
4This is no attribute for this: you have to read the documentation for each item your are interested in. You can't make something thread safe simply by adding an attribute to it. That's like taking an orange and putting a sticker on it that says, "Apple".
Of course, the same thing is true for serialization and it didn't stop them there, but still: no attribute. Read the docs.
I got my back on Joel Coehoorn on this one, would be easy to 'fake' such claims.
Why not add the threadsafe text in the function description
There is no use case scenario of making a class synchronized, as far as method is concerned you use following coding style:
using System.Runtime.CompilerServices;
[MethodImpl(MethodImplOptions.Synchronized)]
void MyMethod()
{
DoSomething();
}
It is possible to make all access to your object serial via IContributeObjectSink/IContextAttribute
although this will suffer from a large performance hit since it will require your object to subclass MarshalByRefObject
,context creation overhead, etc...