How does locking (syncRoot) make sense in a static method?

The following code is extracted from the template (Windows Identity Foundation SDK) used by MS to create a new security token service website.

public static CustomSecurityTokenServiceConfiguration Current  
{  
    get  
    {
        var key = CustomSecurityTokenServiceConfigurationKey;

        var httpAppState = HttpContext.Current.Application;

        var customConfiguration = httpAppState.Get(key)
            as CustomSecurityTokenServiceConfiguration;  

        if (customConfiguration == null)
        {  
            lock (syncRoot)
            {
                customConfiguration = httpAppState.Get(key)
                    as CustomSecurityTokenServiceConfiguration;  

                if (customConfiguration == null)
                {
                    customConfiguration =
                        new CustomSecurityTokenServiceConfiguration();  
                    httpAppState.Add(key, customConfiguration);
                }
            }
        }    
        return customConfiguration;  
    }
}

I am relatively new to multithreaded programming. I assume the reason for the operator lockis to make this code thread safe if two web requests arrive at the website at the same time.

However, I would think that using lock (syncRoot)it makes no sense, because it syncRootrefers to the current instance that this method is working on ... but it is a static method!

How does that make sense?

+3
1

# lock , , . syncRoot. syncRoot, , CustomSecurityTokenServiceConfiguration . , . lock { ... } . , , if (customConfiguration == null) lock. .

+6

Source: https://habr.com/ru/post/1746012/


All Articles