Re: When is "volatile" used instead of "lock" ?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



It only makes sense to think that I was implying that if I had actually
made an assertion of "no JIT optimizations of members between
Enter/Exit".
Agreed, you never explicitly said that.

So, you're not asserting there can be no JIT optimizations of members
between
Enter/Exit that aren't explicitly involved in a volatile operation, and
you're agreeing that:

int memberVariable;
//....


int a;
lock (someReference) {
a = memberVariable;
}

Console.WriteLine(a);

could, by my understanding of the memory model, but reordered to:

int a;
lock (someReference) {

}


a = memberVariable;
Console.WriteLine(a);

....which is what I was trying to get at originally with "the lock statement
surrounding access to a member doesn't stop the compiler from having
optimized use of a member by caching it to a register"...yes, the compiler
*could* assume that all members within the lock statement block are likely
accessible by multiple threads (implicit volatile); but that's not its
intention and it's certainly not documented as doing that". To which, your
response was quoting 335 12.6.5's "Acquiring a lock
(System.Threading.Monitor.Enter or entering a synchronized method) shall
implicitly perform a volatile read operation, and releasing a lock
(System.Threading.Monitor.Exit or leaving a synchronized method) shall
implicitly perform a volatile write operation." In fairness, by "lock
statement surrounding access to a member" I was intending a member within
the block (i.e. lock(...){member=something;}) not the reference being
locked; so I can see how the conversation side-tracked (I don't dispute the
reference sent to Monitor.Enter/Monitor.Exit is considered volatile, but
you'd think the IL generated for "lock" would have instructions with a
volatile prefix). This made me incorrectly think you were implying all
member access between Monitor.Enter and Monitor.Exit was volatile and not
subject to JIT optimizations. But, I think we agree we were talking about
different things.

A variation in your example would be

int a;
lock(someReference) {
memberVariable = a;
}
Console.WriteLine(memberVariable);

being optimized to

int a
lock(someReference){
}
memberVariable = a;
Console.WriteLine(memberVariable);

....which shouldn't occur if memberVariable were declared with "volatile".

One thing it may be worth considering is what the authors of the spec
*intended*. If everyone agrees on that, then at least the spec can
hopefully be improved in the future to reflect it.

Well, it may be moot at this point what was intended in the spec. I doubt
the .NET JIT can change what it's currently doing should "what's intended"
be different that what was implemented. But, I do agree. I think it's
vital to have a coherent unambigious quantifiable and qualifiable spec so
"compliance" means something so developers *can* develop truly platform
independant code. (*can* because they'll always be able to incorrectly
write code to works on only one platform). Where platform is as granular as
OS/architecture combination.


.



Relevant Pages

  • Re: When is "volatile" used instead of "lock" ?
    ... The sample clearly shows that putting a member between Enter/Exit does not ... Within Enter/Exit, if there are no other volatile operations involved, ... If A acquired a lock and D releases it, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is "volatile" used instead of "lock" ?
    ... you're not asserting there can be no JIT optimizations of members ... lock { ... surrounding access to a member doesn't stop the compiler from having ... memory references can move to later than a volatile write or earlier ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: When is "volatile" used instead of "lock" ?
    ... The sample clearly shows that putting a member between Enter/Exit does not ... that only the locked object is the volatile ... Nah - I'll fix it just using an extra lock. ... uncomment the volatile on continueRunning and it runs as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: what is the point of volatile?
    ... me to be a lazy way of protecting a private data member without having ... I don't know why you call it "lazy". ... Reads from a volatile member are guaranteed to see previous writes to that variable. ... reads from a volatile variable guarantee that all writes prior to the latest write to that variable are visible. ...
    (comp.lang.java.help)
  • Serializing changes to parmlib/proclib
    ... We are beginning to customize our z./OS 1.7. ... Is there a way to lock member so that only one person ... For IBM-MAIN subscribe / signoff / archive access instructions, ...
    (bit.listserv.ibm-main)