Why are static methods in .NET Framework classes always thread safe?

I noticed the following expression in most places in the .Net documentation.

Question : What is the secret of this? I don't think a static class is always thread safe. My question is about the standard classes available in the .NET Framework, not the custom classes created by the developers.

Thread Safety Note in all .Net Framework Classes documentation

Will the GetString method in the static class below be thread safe just because the method is a static method?

public static class MyClass
{
    static int x = 0;

    static MyClass()
    {
        x = 23;
    }

    public static string GetString()
    {
        x++;
        return x.ToString();
    }
}

      

+3


source to share


3 answers


The above methods are not thread-safe just because they are static, but because they are specifically designed to be thread-safe. Thread protection is often required, but this is usually necessary for static methods, as any state they mutate is shared between threads.



The chosen sampling method is not thread-safe because it changes state that is shared between threads without a synchronization mechanism.

+9


source


The easiest way to use the non-threadafe instance method without any threading problems is to make that instance visible to only one thread (just don't reference it statically or anywhere else where a thread other than this who created access to it). Indeed, it happens more than 90% of the time without much effort to do so.

The second is the easiest way to associate a lock object with an instance (whether by using it as a lock object or having fields for both it and the lock object within the same scope) and ensure that all access locks are correct.

With a static method, we don't have this option because there is no such instance and any thread could potentially call it or another static method that collides at any time. It may not even be from the same author's code. We also cannot guarantee that other code uses the lock objects (blocks) that we have to use with it.

Therefore, for this reason, a static method that is not thread safe is very limited; are pretty much only applicable to private static methods used in very limited cases, with these constraints ensuring that it is only called by one thread at a time.



Hence, with all public static methods, one can make sure that they are thread safe (or else they very clearly document that they are not thread safe, and there is an excuse for such a strange thing).

In fact, you will find that there are actually many instance methods that are documented as "not thread safe". The reasons they are listed as "not threadsafe" are:

  • If the author hasn't gotten around to verifying their thread safety, they'd better not ask for something they're not 100% sure of.
  • Wrong in this direction is safe; worst of all is someone adding their own synchronization to add thread safety they didn't need, which doesn't really break anything.
  • Since they haven't documented the method as thread-safe, they are free to migrate to the non-thread-safe approach in a later version.
+2


source


The specific examples you link to are for thread safety. That is, they allow concurrent access without deadlocks or race conditions.

This may not be the case in all cases of all classes, as Microsoft decided to clearly state which methods are thread safe and which should not avoid any ambiguity.

See the final paragraph - . NET class library

All public static members (methods, properties, fields, and events) in the .NET Framework support concurrent access in a multithreaded environment. This way, any static member of the .NET Framework can be called simultaneously from two threads without encountering race conditions, deadlocks, or crashes.

For all classes and structures in the .NET Framework, check the Thread Safety section of the API reference documentation to determine if it is thread safe. If you want to use a class that is not thread safe in a multithreaded environment, you must transfer an instance of the class with code that provides the necessary synchronization constructs.

+1


source







All Articles