Is there an attribute for marking a class or method that should be thread safe in .NET?

In .NET, how can I find out if a class or method is thread safe or not? Is it not safe by default?

+5
source share
5 answers

This is not an attribute for this: you should read the documentation for each item you are interested in. You cannot do something thread safe just by adding an attribute to it. It's like taking an orange and sticking a sticker on it that says: "Apple."

Of course, the same is true for serialization, and that did not stop them there, but still: no attribute. Read the docs.

+6
source

, "" .

+1

IContributeObjectSink/IContextAttribute, , MarshalByRefObject, .....

+1

, .

, , Thread Safe ™:

void Add( something);
void Remove(index);
int GetCount();
something GetElementAt(index);

:

for 1 to 100 do
 list.Add(12);

:

while( list.GetCount() >0)
{
    list.Remove(0);
}

The code above will fail (sooner or later), because the list may change between the moment you call GetCountandRemove

+1
source

There is no scenario for using a synchronized class, because the following coding style is used for the method:

using System.Runtime.CompilerServices;
[MethodImpl(MethodImplOptions.Synchronized)]   
void MyMethod()   
{   
DoSomething();   
}  
-1
source

All Articles