Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!feed.cnntp.org!news.cnntp.org!not-for-mail From: Anthony Williams Newsgroups: comp.programming.threads Subject: Re: forcing the compiler to reload from memory with c++0x References: <4d3cee42$0$1209$426a74cc@news.free.fr> <1d21ad0a-db55-460e-aeb6-66f6d19369ea@i13g2000yqe.googlegroups.com> <4d3e2e3f$0$21517$426a34cc@news.free.fr> <4D40A4C7.16E6F9AE@web.de> <69d82db7-34be-4de7-86c4-2f39cc1e6df3@m13g2000yqb.googlegroups.com> <4d4221e9$0$17247$426a74cc@news.free.fr> <947b33d5-2509-4f0c-9256-a7c3de35578e@24g2000yqa.googlegroups.com> <87mxmlxxry.fsf@justsoftwaresolutions.co.uk> <87oc6yxdcw.fsf@justsoftwaresolutions.co.uk> <87sjw8w4b5.fsf@justsoftwaresolutions.co.uk> <4D480800.1C5132D7@web.de> Date: Tue, 01 Feb 2011 13:51:54 +0000 Message-ID: <87k4hjx3ph.fsf@justsoftwaresolutions.co.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:KWZuijsji+ba0uqwX+DO8VA0SoQ= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 51 Organization: CNNTP NNTP-Posting-Host: 802f0b96.read.cnntp.org X-Trace: DXC=_]8Ho9[=cnKT4_6BS`k^@IWoT\PAgXa?Af8aof 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