[cpp-threads] C++ memory model
Anthony Williams
anthony at justsoftwaresolutions.co.uk
Mon Sep 14 11:18:10 BST 2009
Mark Batty wrote:
> In the process of this formalization and proof some parts of the draft
> standard seemed difficult to understand or inconsistent - mostly
> relatively local points. I've written a note describing the
> formalization and some specific suggestions for local clarifications
> and improvements to the standard. There's a copy here:
>
> http://www.cl.cam.ac.uk/~mjb220/cpp/cpp.pdf
Re section 3.5, the intent of clause 29.3p8 of the working draft is to
disallow "out of thin air" values. In the example given, the value 42
can be stored into x by thread 2 iff it is stored into x by thread 2!
The intent is that the execution (or not) of a particular operation
cannot be conditional on its own execution.
Likewise, the intent of clause 29.3p9 is to ensure that code like the
following works as expected --- values must eventually become visible to
other threads:
std::atomic_bool flag(false);
Thread 1:
while(!flag.load()); // busy wait for flag
Thread 2:
flag.store(true); // wake thread 1
I do not think your suggest clarification helps here. The point is that
compiler cannot optimize the loop to
bool const local_flag=flag.load();
if(!local_flag) while(true);
Anthony
--
Author of C++ Concurrency in Action | http://www.manning.com/williams
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
More information about the cpp-threads
mailing list