[cpp-threads] Yet another visibility question

Peter Dimov pdimov at mmltd.net
Wed Dec 20 14:15:59 GMT 2006


Boehm, Hans wrote:

> If I have
>
> if (x.load_raw()) {
>     y = 1;
> }
> y = 1;
>
> Is there a dependence?

To answer the question: yes, there is a dependence. Different values of x 
can cause one of the two executions

y = 1;

or

y = 1;
y = 1;

to take place. So the first store to y is dependent on the x load. This 
variation is more subtle:

r1 = x.load_raw();

if( r1 )
{
    y = 1;
}

if( !r1 )
{
    y = 1;
}

assuming that the two ifs can be hidden away in function calls. The store to 
y is not dependent.

The refcounting example doesn't have this problem; the actions that are 
taken when the count drops to zero are provably dependent since the result 
of the atomic is not available to other portions of the code and can't 
cancel the dependence. This relies on atomic loads being somewhat "volatile" 
as far as data flow analysis is concerned, though. That is, it relies on:

if( x.load_raw() )
{
    y = 1;
}

if( !x.load_raw() )
{
    y = 1;
}

not being equivalent to the above example, even if the compiler can prove 
that x is not modified during the execution of this snippet.

All this seems more trouble than it's worth. :-)




More information about the cpp-threads mailing list