volatile variables in D....

Forest Ray disto at flying-guillotine.com
Thu Apr 12 00:04:34 PDT 2007


I am NOT asking for an atomic increment or access.  What volatile guarantees is that the variable is always read from memory or written to memory for each access.  The ISR routine increments the timer variable
every 1 ms.  The user space application (which will be getting interrupted every 1 ms) checks the variable to schedule events...this is not relying on atomic access.

== Quote from David B. Held (dheld at codelogicconsulting.com)'s article
> In fact, C++'s 'volatile' isn't guaranteed to do the right thing because
> C++ doesn't define a memory model which would be necessary to define
> what The Right Thing even is.  Java made the same mistake at first, but
> later fixed the standard to properly define 'volatile'.  While setting
> up memory barriers etc. to make volatile work properly might seem like a
> quick and easy solution, I think we need to think carefully about what
> multithreading primitives really give the most benefit.
> In this case, you basically want volatile to give you a cheap form of
> atomic increment.  However, I would suggest that if you really need the
> accuracy of atomic increment, then you should consider a primitive like
> CAS instead:
> int count = 0;
> asm
> {
>      MOV EBX, &count;
>      MOV EAX, [EBX];
> Retry:
>      MOV EDX, EAX;
>      INC EDX;
>      CMPXCHG [EBX], EDX;
>      JNZ Retry;
> }
> My x86 asm is pretty rusty, so this might be completely bogus code, but
> it should give you an idea of what I mean.
> Dave




More information about the Digitalmars-d mailing list