[cpp-threads] High-level vs. low-level
Peter Dimov
pdimov at mmltd.net
Thu Jul 13 14:05:40 BST 2006
In this message, I will try to explain in more detail why I think the "low
level" syntax is better than the "high level" syntax. By low level syntax I
mean
atomic_store_rel( &x, 1 );
r1 = atomic_load_acq( &x );
and by high level syntax I mean
x = 1;
r1 = x;
where x is suitably declared as atomic.
First, consider the programming community, in particular, the part that
needs to read code. When encountering
x = 1;
the programmer needs to look up the declaration of x in order to infer the
semantics of the assignment. In some cases (amortized constant) he'll also
need to look up the C++ memory model specification. Not a big deal,
admittedly.
The bigger problem is that after looking up the necessary information to
infer the precise semantics of x = 1, our programmer still doesn't know what
is _the intent_ of the assignment and how it came to be. It could be that
this particular statement never races but x is atomic because some of its
other manipulations do involve races. It could be that there is a race, but
no memory synchronization is needed. It could be that the code depends on
the release barrier for correctness. Finally, it could be that the code was
written as-is with x being an ordinary variable, then x was changed to
atomic<> as a bug fix without any kind of careful analysis of its
correctness.
All this isn't mere speculation; currently we have the exact same situation
when x is volatile, and the above problems do occur quite regularly.
Second, consider the lock-free research, in particular its practical
applicability.
The current de-facto standard, as far as I've seen, is to describe
algorithms in the "high level" syntax, assuming a sequential consistency
memory model. There is usually a small print note along the lines of "the
reader is expected to insert the appropriate memory barriers at the
appropriate places to make the described algorithm work on real-world
hardware". Usually, the appropriate barriers and places aren't described
anywhere in the paper.
One approach to bridge this gap between the research community and the C++
programming community is to provide a way to achieve sequential consistency
by using an ordinary syntax, but this obviously leads to suboptimal
implementations.
The alternative is to provide a standard vocabulary for expressing the
"appropriate memory barriers at the appropriate places" and hope that it is
adopted by the research comminity. In my opinion, the low-level syntax can
serve as such a vocabulary.
Thanks for reading. :-)
More information about the cpp-threads
mailing list