473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Double checked locking

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
3 3664
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
16
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
11
by: John Salerno | last post by:
Ok, I'm having this problem again. The first I was able to use the debugger to see that it was being called twice, so I fixed that. This time, though, the debugger does not show it...
3
by: JohnQ | last post by:
SomeObject* instance() // flawed way { static SomeObject* instance = NULL; LockThing(); // locking even when not required if(instance == NULL) // instance may still be NULL when another thread...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
8
by: claudiu | last post by:
Hi, I have a question on the double checked locking. The classic implementation (shamelessly copied from Scott Meyers' and Andrei Alexandrescu's article) class Singleton { public: static...
0
by: George2 | last post by:
Hello gurus, For the wellknown Double-Checked Locking pattern, http://www.ddj.com/184405726?pgno=1 Step 1: Allocate memory to hold a Singleton object. Step 2: Construct a Singleton object...
1
by: =?Utf-8?B?WVhR?= | last post by:
When double-click the listview item with checkbox (View = Details), the checkbox will be checked or unchecked, how to disable this behavior? thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.