In .NET how can I know if a class or method is thread safe or not? Is it by default not thread safe?
This 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.
No, and it would be pointless.
Let's assume I have a thread safe List, it has three Thread Safe™ methods:
void Add( something);
void Remove(index);
int GetCount();
something GetElementAt(index);
Thread one:
for 1 to 100 do
list.Add(12);
Thread two and three:
while( list.GetCount() >0)
{
list.Remove(0);
}
The code above will crash (sooner or later) because the list might change between the time you call GetCount
and Remove
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With