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

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



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
.



Relevant Pages

  • Re: doubts in multi threaded application (win32 console + mfc)
    ... Thank you all (Scott McPhillips,Carl Daniel,Alex and aao) for your prompt ... What is maximum size for stack memory in multi threaded ... It highly depends on the usage and lifetime of an object, ...
    (microsoft.public.vc.language)
  • Re: Array scope
    ... `auto' variables are allocated on a stack, ... implementations feel free to modify the stack memory ... addressed by your bogus pointer. ...
    (comp.lang.c)
  • Re: doubts in multi threaded application (win32 console + mfc)
    ... I am creating objects in stack memory. ... In which memory (Stack or Heap) I have to create objects to improve the performance? ... CString performance will probably improve if you upgrade to VC++ 2005. ...
    (microsoft.public.vc.language)
  • Re: how much memory a pthread can define a variable in a stack?
    ... stack consumers. ... > option for a thread to get more stack memory? ... recommend 8Kb maximum for a library function and 32Kb as an absolute ...
    (comp.unix.programmer)
  • Re: OT: UK okays warrantless remote hacking of PCs
    ... -- suggests to me that the onus for writing good quality code is still much more a function of the individual writing that code rather than the particular language they use: I wouldn't inherently trust a guy writing nuclear reactor control code any more if he's using Java than if he's using C! ... stacks don't overflow into code. ... It's not so much stacks overflowing into code, as programs writing to data in a way that corrupts the return address on the stack. ... If that data has been provided by an attacker - e.g. it might be part of what is meant to be a URL, or an image, or something - then the attack succeeds. ...
    (sci.electronics.design)