Home >Backend Development >C++ >Is C#'s Static Constructor Sufficient for Thread-Safe Singleton Implementation?

Is C#'s Static Constructor Sufficient for Thread-Safe Singleton Implementation?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-16 11:36:58149browse

Is C#'s Static Constructor Sufficient for Thread-Safe Singleton Implementation?

Thread safety of C# static constructor in singleton mode

The singleton pattern is a common design pattern in software development, ensuring that a class has only one instance in the application domain. In C#, a static constructor is responsible for initializing the static members of a class, including instances in a singleton implementation.

Question:

Is the following C# singleton implementation thread-safe?

<code class="language-C#">public class Singleton
{
    private static Singleton instance;

    private Singleton() { }

    static Singleton()
    {
        instance = new Singleton();
    }

    public static Singleton Instance
    {
        get { return instance; }
    }
}</code>

Answer:

Yes, the static constructor in this implementation guarantees thread safety during the initial construction of the singleton instance. According to Microsoft documentation, static constructors in C# are guaranteed to run only once in each application domain before any instance of the class is created or static members are accessed.

However, it is important to note that this thread safety only applies to the initial construction of the instance. It does not guarantee that subsequent use of the instance will be synchronized. To ensure complete thread safety, additional synchronization mechanisms are required.

One way to achieve thread synchronization is to introduce a static mutex to control access to the singleton instance:

<code class="language-C#">public class Singleton
{
    private static Singleton instance;
    private static System.Threading.Mutex mutex;

    private Singleton() { }

    static Singleton()
    {
        instance = new Singleton();
        mutex = new System.Threading.Mutex();
    }

    public static Singleton Acquire()
    {
        mutex.WaitOne();
        return instance;
    }

    public static void Release()
    {
        mutex.ReleaseMutex();
    }
}</code>

This implementation requires that each call to Acquire() be paired with a call to Release(), ensuring that only one thread can access the singleton instance at a time. By taking this approach, thread safety is achieved not only during initial construction, but also during subsequent use of the instance.

The above is the detailed content of Is C#'s Static Constructor Sufficient for Thread-Safe Singleton Implementation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn