Re: Inter-process atomic operations

Tech-Archive recommends: Fix windows errors by optimizing your registry



Nadav wrote:
2. The ‘volatile’ keyword assures that the volatile variable would use a register for interaction, this guarantee atomic interactions, BUT, when

Don correctly points out that (typically) volatile ensures that a variable is *not* stored in a register.

However, I'd like to point out that the volatile keyword doesn't ensure *anything*. The standard allows compliant compilers to completely ignore that keyword.

Don't rely on it for anything. The best it can do, anyway (if used comprehensively), is help generate warnings when you try to do something you didn't realize was dangerous (e.g. using a non-volatile function on a volatile class object).

In practice, in the vast majority of compilers, all it does is disable optimizations that make the assumption that a variable can't change outside the scope of the code that's using it.
--
Ray
.



Relevant Pages

  • Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
    ... It doesn't mean that (volatile int*) cast is bad, ... I would agree that fixing the compiler in this case would be a good thing, ... bad register allocation. ... still quite possible to find cases where it did some optimization (in this ...
    (Linux-Kernel)
  • Re: [PATCH 0/24] make atomic_read() behave consistently across all architectures
    ... gcc bugzilla bug #33102, for whatever that ends up being worth. ... x++ becomes a register load, ... volatile we can increment the memory directly. ... access memory mapped I/O registers where a RMW operation could be ...
    (Linux-Kernel)
  • Is this code MT-Safe?
    ... void start ... Does _bRunning need to be tagged as volatile? ... m_pList to be cached in a register; what if the threads also called ... Do non-inlined function-calls change this behavior? ...
    (comp.lang.cpp)
  • Re: [patch] spinlocks: remove volatile
    ... I haven't been following this thread very closely but your assault on volatile is inappropriate. ... You cannot have an operand that describes a register class with a single member if that register is in the clobber list; there is simply no way to specify that an input operand is modified without explicitly specifying it as output. ... If all the output operands are for that purpose, ... volatile is _necessary_ for the asm to prevent the compiler from deleting it for this reason. ...
    (Linux-Kernel)
  • Re: When is "volatile" used instead of "lock" ?
    ... lock on the same reference you're ... volatile); but that's not its intention and it's certainly not documented as ... 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. ...
    (microsoft.public.dotnet.languages.csharp)