G
Guest
This code uses lazy initialization and double checked locking to read urls
associated with a retailer from a map.
Will the code work? I'm not sure how volatile applies to references.
private static volatile Dictionary<string, string> urlMap_;
private static object syncRoot = new Object();
public static string GetUrl(string retailerId)
{
string url;
urlMap_.TryGetValue(retailerId, out url);
if (url == null)
{
lock (syncRoot)
{
urlMap_.TryGetValue(retailerId, out url);
if (url == null)
{
url = .. //somehow obtain the url
urlMap_.Add(retailerId, url);
}
}
}
return url;
}
associated with a retailer from a map.
Will the code work? I'm not sure how volatile applies to references.
private static volatile Dictionary<string, string> urlMap_;
private static object syncRoot = new Object();
public static string GetUrl(string retailerId)
{
string url;
urlMap_.TryGetValue(retailerId, out url);
if (url == null)
{
lock (syncRoot)
{
urlMap_.TryGetValue(retailerId, out url);
if (url == null)
{
url = .. //somehow obtain the url
urlMap_.Add(retailerId, url);
}
}
}
return url;
}