Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 14 Jul 2007 19:42:32 +0100
Peter Ritchie [C#MVP] <prsoco@xxxxxxxxxxxxxxxxx> wrote:
<snip>
Also, why should it distinguish between stack and heap memory? Why should
stack memory be disregarded with respect to these rules? What if I were
writing an synchronous method that called an asynchronous method that
accepted a memory reference? If I pass it a reference to stack memory,
there's nothing I can do to tell the compiler that variable should be
treated as volatile by the JIT. For example:
public int Method( )
{
double value1 = 3.1415;
int value2 = 42;
IAsyncResult result = BeginBackgroundOperation(ref value1, ref value2);
Do you have an example of this sort of asynchronous behaviour which
actually works? Delegate.BeginInvoke has different behaviour for
ref/out parameters than for value parameters - you fetch them
separately when you call EndInvoke. In other words it doesn't update
the values you provide in the call to BeginInvoke. That's not to say
that other framework calls *don't* work in the way you describe - but
I've never seen one. Do you have an example?
The problem is - what would you expect to happen if Method() just
returned at this point, thus popping the stack frame? It would be
*hugely* unsafe to let another thread start arbitrarily writing to
stack data for a different thread. I don't *believe* it's possible to
do in safe C# code, although it could be possible with unsafe code.
I think it's fundamentally a bad idea for one thread to access memory
in another thread's stack (except in very controlled environment such
as a debugger) - I think it's reasonable to put that (explicitly, mind)
outside the restrictions of the locking protocol.
// Sit in a loop waiting for up to 250ms at a time
// doing something with the double value...
do
{
value2 = 5;
// doubles aren't atomic, we need to use
// VolatileRead to read the "latest written" value and
// because BeginBackgroundOperation uses
// Thread.VolatileWrite(ref double).
Even if doubles were treated atomically, that wouldn't mean you'd still
get the "latest written" value. It just means you'd either get the new
value or the old one, not a "half way house".
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- 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: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Jon Skeet [C# MVP]
- Re: When is "volatile" used instead of "lock" ?
- From: Peter Ritchie [C#MVP]
- Re: When is "volatile" used instead of "lock" ?
- Prev by Date: Re: opening more than once
- Next by Date: Example of TYPED INTERFACE ?
- 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
|