Csharp/C Sharp/Development Class/Volatile

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Access Reordering and Volatile:Use of volatile

using System;
public class Singleton
{
    static object sync = new object();
    
    static volatile Singleton singleton = null;
    
    private Singleton()
    {
    }
    
    public static Singleton GetSingleton()    
    {
        if (singleton == null)
        {
            lock(sync)
            {
                if (singleton == null)
                singleton = new Singleton();
            }
        }
        
        return(singleton);
    }
}