Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1820

Re: forcing the compiler to reload from memory with c++0x

From Anthony Williams <anthony.ajw@gmail.com>
Newsgroups comp.programming.threads
Subject Re: forcing the compiler to reload from memory with c++0x
References (8 earlier) <ihv8t6$bmr$1@news.eternal-september.org> <87oc6yxdcw.fsf@justsoftwaresolutions.co.uk> <ii745u$5q3$1@news.eternal-september.org> <87sjw8w4b5.fsf@justsoftwaresolutions.co.uk> <4D480800.1C5132D7@web.de>
Date 2011-02-01 13:51 +0000
Message-ID <87k4hjx3ph.fsf@justsoftwaresolutions.co.uk> (permalink)
Organization CNNTP

Show all headers | View raw


Alexander Terekhov <terekhov@web.de> writes:

> Anthony Williams wrote:
> [...]
>> You can of course make your atomic variables volatile too, and then they
>> DO become observable behaviour.
>
> Observable as in what? Memory mapped I/O variables and signal handlers
> aside, you can't mean debuggers and/or asm listings. The following
> programs

The C++ Standard defines "observable behaviour" to be the set of
accesses to volatile variables and I/O performed by the program.

The only requirement on a compiled program is that it produce the same
set of observable behaviour as that required by the abstract machine,
which includes accesses to volatile variables.

How you are supposed to observe the accesses to volatile variables is
not specified.

> int main() {
>   volatile int i;
>   // volatile write = has no observable effect
>   i = 1;
>   // volatile read = has no observable effect
>   int n = i;
>   // output: 1
>   std::cout << n;
> }
>
> int main() {
>   // output: 1
>   std::cout << 1;
> }
>
> can be translated into the same executable code.

FYI, gcc doesn't treat them the same, even with -O3: the first gets
translated to a store of 1 to a stack variable followed by a load from
that stack variable into a register, then a call to write that to
cout. The second just gets translated into a load of 1 into a register,
then a call to write that to cout. If you omit the "volatile" from the
first example then it is translated the same as the second.

Anthony
-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++0x thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

Back to comp.programming.threads | Previous | NextPrevious in thread | Find similar


Thread

Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-01 14:17 +0100
  Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 14:45 +0100
    Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-02 07:02 -0800
      Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 16:33 +0100
  Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-01 16:32 +0100
    Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-02 12:13 +0100
      Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-02 04:54 -0800
    Re: forcing the compiler to reload from memory with c++0x Dmitriy Vyukov <dvyukov@gmail.com> - 2011-02-01 21:02 -0800
    Re: forcing the compiler to reload from memory with c++0x Joshua Maurice <joshuamaurice@gmail.com> - 2011-02-01 15:19 -0800
  Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-01 13:51 +0000

csiph-web