Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2004

Tip: Looking for answers? Try searching our database.

.NET Memory Model

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shapiro - 20 Dec 2004 04:41 GMT
I am looking for a detailed technical documentation of the .NET memory
model. I am to implement a double lock checking(DLC) in C# to avoid a race
condition in a sure proof way for a singleton class. I know in JAVA DLC is
not always sure proof because of the Java memory model implemented
differently by different vendors.

Thanks for the info.
Shapiro
Mujtaba Syed - 20 Dec 2004 05:46 GMT
If you want to synchonize access to a critical section, you could use the
System.Threading.Monitor class (also available through the lock keyword in
C#).

Mujtaba.

> I am looking for a detailed technical documentation of the .NET memory
> model. I am to implement a double lock checking(DLC) in C# to avoid a race
[quoted text clipped - 4 lines]
> Thanks for the info.
> Shapiro
Shapiro - 20 Dec 2004 06:38 GMT
My question is not about what construct to use for synchronization( I know
the synchronization classes/objects available in .NET), its finding a thread
safe way to implement DLC.

Shapiro.

> If you want to synchonize access to a critical section, you could use the
> System.Threading.Monitor class (also available through the lock keyword in
[quoted text clipped - 10 lines]
> > Thanks for the info.
> > Shapiro
Daniel O'Connell [C# MVP] - 20 Dec 2004 06:01 GMT
> I am looking for a detailed technical documentation of the .NET memory
> model. I am to implement a double lock checking(DLC) in C# to avoid a race
> condition in a sure proof way for a singleton class. I know in JAVA DLC is
> not always sure proof because of the Java memory model implemented
> differently by different vendors.

There is no proof, but a fair amount of suspicion that double check locking
may not be safe. However, there are better ways to write singeltons

see http://www.yoda.arachsys.com/csharp/singleton.html
> Thanks for the info.
> Shapiro
Shapiro - 20 Dec 2004 06:43 GMT
The examples listed on the  url you provided are all not thread safe in a
way(in the DLC you should decorate the insttance variable with the volatile
keyword) and also it does not tell me anything about the .NET memory model. I
am interested in knowing the .NET memory model so I can implement DLC based
on the memory model.

Shapiro.

> > I am looking for a detailed technical documentation of the .NET memory
> > model. I am to implement a double lock checking(DLC) in C# to avoid a race
[quoted text clipped - 8 lines]
> > Thanks for the info.
> > Shapiro
Daniel O'Connell [C# MVP] - 20 Dec 2004 07:53 GMT
> The examples listed on the  url you provided are all not thread safe in a
> way(in the DLC you should decorate the insttance variable with the
[quoted text clipped - 4 lines]
> based
> on the memory model.

I believe the memory model is the same as Java, that is, vauge as can be.
DLC should work on windows on 32bit processors with volatile, but may not on
other platforms, but I couldn't speak of mono.

You can look up the CLR spec, ECMA has it posted and Chris Brummes articles
on blogs.msdn.com may have some information on the model.

FWIW, if you had read the article you would see that two of the options are
inherently thread safe due to runtime locking, while three are not. From
your comments I assume you just scanned the samples and ignored the text.
Jon Skeet [C# MVP] - 20 Dec 2004 09:20 GMT
> I believe the memory model is the same as Java, that is, vauge as can be.
> DLC should work on windows on 32bit processors with volatile, but may not on
> other platforms, but I couldn't speak of mono.

I believe with the variable being volatile, it should work on all
platforms. Without the variable being volatile it *may* still work on
the MS implementation, but I wouldn't like to rely on it.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Daniel O'Connell [C# MVP] - 20 Dec 2004 10:58 GMT
>> I believe the memory model is the same as Java, that is, vauge as can be.
>> DLC should work on windows on 32bit processors with volatile, but may not
[quoted text clipped - 4 lines]
> platforms. Without the variable being volatile it *may* still work on
> the MS implementation, but I wouldn't like to rely on it.

Should...but unless its guarenteed in the spec(and implemented properly most
of the time), I would be concerned with using it that way. When it comes to
threading I don't like relying on should, ;).
Jon Skeet [C# MVP] - 20 Dec 2004 17:17 GMT
> >> I believe the memory model is the same as Java, that is, vauge as can be.
> >> DLC should work on windows on 32bit processors with volatile, but may not
[quoted text clipped - 8 lines]
> of the time), I would be concerned with using it that way. When it comes to
> threading I don't like relying on should, ;).

No, that's "should" as in "I believe the spec guarantees it". The
volatile write (on construction) makes sure that all the work done in
the constructor occurs before the write to the volatile variable, and
the volatile read (on check) makes sure that all reads after the check
occur after the read (and thus after the write).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Daniel O'Connell [C# MVP] - 20 Dec 2004 23:12 GMT
>> >> I believe the memory model is the same as Java, that is, vauge as can
>> >> be.
[quoted text clipped - 18 lines]
> the volatile read (on check) makes sure that all reads after the check
> occur after the read (and thus after the write).

Well, if the spec guarantees it, than its fairly safe(assuming that most
implementations actually get it right, which is another issue entirely).

It still seems troubing.
Jon Skeet [C# MVP] - 20 Dec 2004 09:18 GMT
> The examples listed on the  url you provided are all not thread safe in a
> way(in the DLC you should decorate the insttance variable with the volatile
> keyword)

Um, no. The ones which the page says are thread-safe *are* thread-safe.
And yes, for DLC you need to make the instance variable volatile.

> and also it does not tell me anything about the .NET memory model. I
> am interested in knowing the .NET memory model so I can implement DLC
> based on the memory model.

Why not just use the versions which *don't* require DLC at all, and are
still very efficient?

For the memory model, see the ECMA CLI standard:
http://www.ecma-international.org/publications/standards/Ecma-335.htm

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

William Stacey [MVP] - 26 Dec 2004 12:32 GMT
Why is everyone saying DLC?  I thought it was DCL (Double-checked locking).
Or are you talking something else?

Signature

William Stacey, MVP
http://mvp.support.microsoft.com

> > The examples listed on the  url you provided are all not thread safe in a
> > way(in the DLC you should decorate the insttance variable with the volatile
[quoted text clipped - 12 lines]
> For the memory model, see the ECMA CLI standard:
> http://www.ecma-international.org/publications/standards/Ecma-335.htm
Jon Skeet [C# MVP] - 27 Dec 2004 09:34 GMT
> Why is everyone saying DLC?  I thought it was DCL (Double-checked locking).
> Or are you talking something else?

I'm just saying it from blindly following the OP. Oops :(

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.