Re: When is "volatile" used instead of "lock" ?
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Thu, 14 Jun 2007 20:45:01 +0200
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.20db8fad270511ef21e@xxxxxxxxxxxxxxxxxxxxxxx
Peter Ritchie [C# MVP] <PRSoCo@xxxxxxxxxxxxxxxxx> wrote:
<snip>
By the same token, 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 especially if that member is declared in a different assembly that
was compiled for this code was written:
lock(lockObject)
{
i = i + 1;
}
Acquiring a lock has acquire semantics, and releasing a lock has
release semantics. You don't need any volatility if all access to any
particular item of shared data is always made having acquired a certain
lock.
If different locks are used, you could be in trouble, but if you always
lock on the same reference (when accessing the same shared data) you're
guaranteed to be okay.
...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 (and it would be pointless, other code knows nothing about this
block and could have optimized use of i by changing its order of access or
caching to a registry).
It certainly *is* documented. ECMA 335, section 12.6.5:
<quote>
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.
</quote>
volatile and lock should be used in conjunction, one is not a replacement
for the other.
If you lock appropriately, you never need to use volatile.
True, when using locks, make sure you do it consistently. And that's exactly why I said that I'm getting suspicious when I see a "volatile" field. Most of the time this modifier is used because the author doesn't understand the semantics of "volatile", or he's not sure about his own locking policy or he has no locking policy at all. Also some may think that volatile implies a fence, which is not the case, it only tells the JIT to turn off some of the optimizations like register allocation and load/store reordering, but it doesn't prevent possible re-ordering and write buffering done by the CPU, note, that this is a non issue on X86 and X64 like CPU's , given the memory model enforced by the CLR, but it is an issue on IA64.
Willy.
.
- Follow-Ups:
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- References:
- RE: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- RE: When is "volatile" used instead of "lock" ?
- Prev by Date: Re: Easy (?!) regular expression -- find line breaks
- Next by Date: Re: Easy (?!) regular expression -- find line breaks
- Previous by thread: RE: When is "volatile" used instead of "lock" ?
- Next by thread: Re: When is "volatile" used instead of "lock" ?
- Index(es):
Relevant Pages
|