[cpp-threads] Memory model question

Boehm, Hans hans.boehm at hp.com
Fri Aug 26 22:48:41 BST 2005


> -----Original Message-----
> From:  Peter Dimov
> Boehm, Hans wrote:
> 
> > With the alternate interpretation, the compiler now also 
> has to prove 
> > that the assignment to global_counter in the initial loop iteration 
> > actually writes back a modified value.  And I don't think 
> that sort of 
> > analysis is something that compilers already do.
> 
> No, this is not true. The compiler doesn't need to prove 
> anything like that; 
> it already has a license to write to global_counter.
> 
> x += y;
You are right.  It also suffices to prove that you are writing
a value that would actually have been written, which does handle this
case.  Thus my concern here was misplaced.  Speculative writes are
really the issue, and they no longer occur if the loop is unrolled
once.
> 
> In fact,
> 
> if( y != 0 )
> {
>     x += y;
> }
> 
> can be transformed to x += y (at source level) under the alternate 
> interpretation, but not under the original.
I don't understand this comment.

Assume a second thread does ++x; and y is always zero.

Original interpretation (no concurrent stores):
No data race in the original.  Transformed version has data race.

Alternate interpretation (no concurrent "modification"):
No data race in the original.  No data race in transformed version
if x is only read in other thread.  But if x is written elsewhere,
the transformation introduces a race.  And you would rarely know that
it isn't.

Hans




More information about the cpp-threads mailing list