By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Double checked locking

P: n/a
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, stringurlMap_;
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;
}

Feb 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Of course, consider that the dictionary is initialized.

"aindrei" wrote:
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, stringurlMap_;
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;
}
Feb 1 '07 #2

P: n/a
Hi,

No, that code won't work. Sure it looks similar to the canonical
double-checked locking implementation in C#, but remember, the
canonical form deals with setting a reference to a single variable.
In that case there is complete control over how that variable is both
read and written. Contrast that with your code. You're not trying
initialize a Dictionary and assign a reference to a variable.
Instead, you're trying to add values to it. You have no control over
how Dictionary.Add and Dictionary.TryGetValue are implemented and what
may go wrong when executed simultaneously. Specifically, I'm
envisioning a scenario where the Dictionary is in the middle of growth
operation while TryGetValue is called.

Brian

On Feb 1, 4:24 pm, aindrei <aind...@discussions.microsoft.comwrote:
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, stringurlMap_;
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;
}

Feb 1 '07 #3

P: n/a
aindrei <ai*****@discussions.microsoft.comwrote:
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.
It's not clear from the docs whether it's safe to have *any* readers
while you've got a writer for Dictionary<K,V>, and that's what you'll
end up with using the code provided.

Any time you have to ask "is it safe?" about threading, the immediate
follow-up question should always be "how expensive would a simpler,
definitely-safe version be"? In this case, the simple version is to
always lock. Have you tried that and found you've got contention? If
obtaining the URL might take a long time, there are alternatives which
might risk fetching a URL twice but without blocking other readers.

I'd definitely steer clear of DCL unless you know you really, really
know you need something particularly cunning. That should be backed up
with profiler evidence, btw.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.