> Thanks for the info.
> Shapiro
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