[cpp-threads] pthreads (was: RE: C++ Connections proposal)

Peter Dimov pdimov at mmltd.net
Tue Apr 26 20:06:22 BST 2005


Boehm, Hans wrote:
> There was substantial discussion of this in the Java context.  I
> don't think anything else is viable. Consider the slight variant:
>
> Thread 1: if (the_world_is_ending) fire_all_missiles=true;
>
> Thread 2: if (fire_all_missiles) the_world_is_ending=true;
>
> It would be a bit surprising to discover that this provoked the
> end of the world for no reason, because the compiler speculated
> the stores.
>
> Note that this is not the same as giving the initial loads of x and y
> acquire semantics of some sort.  We are just guaranteeing that if
> there is no race in the original code, the compiler won't introduce
> one.  Hence there is no way to distinguish a load with acquire
> semantics from a plain load.  Hence I think the compiler cost in
> this case is essentially zero; no compiler in its right mind would
> perform the problematic transformation anyway, I think.

I can think of one example where a compiler could (easily?) have decided to 
do it.

    a[0] = true;
    a[1] = true;
    a[3] = true;

    if( b[2] ) a[2] = true;

A compiler may decide to transform this to:

    bool tmp = a[2];
    *(uint32*)a = 0x01010101;
    if( !b[2] ) a[2] = tmp;

or even (if byte accesses are particularly expensive):

    uint32 tmp = a;
    a = 0x01010101;
    if( (b & 0x0000FF00) == 0 ) a = tmp | 0x01010001;

(where a and b are shorthands for the uint32 aliasing the arrays.)

This is similar to the read-modify-write example, but here the compiler can 
be said to have a legitimate license to modify a[2]. 





More information about the cpp-threads mailing list