Many optimizing compilers will make things work, not because they
should work, but because optimizers like to remove operations and that
tends to make things more atomic. Plus, gcc, for example, tends to
insert memory barriers in places, like at function calls. In many
cases, things can work because datastructures happen to be stable
enough, even in light of weak ordering. However, all this is difficult
to predict -- especially if you want your app to be portable across
compilers and CPUs.
Occasionally, I've seen people comment that you have to let these
things run a really long time in order to see failure, but that's not
usually the case. Usually, if a test fails to fail (!) it's probably
because your compiler has done something, or you just haven't found a
case where it fails. Since absence of proof is not proof of absence,
and there were a lot of doubters out there, I wrote a counterexample
that failed on a standard PPC mac. You can find it if you search the
archives. I'm not sure anyone ever scrutinized it (even me -- I wrote
it super fast), so it may have done what it did due to a bug, but if
you doubt weak ordering I assure you very smart people have spent a
long time building it into the next revision of c++ and I don't think
they would have done that if it weren't necessary. But you can look at
my code if you doubt it, and if it happens to run on your machine,
you'll have to tweak it or take my word for it, as I no longer have
the machine that code used to run on.
bjorn
On Oct 28, 2008, at 9:06 AM, oja_i_i wrote:
> Hi,
>
> Can any of you advise a good test case that would demonstrate memory
> barriers are needed in a lock-free ring buffer ? I mean: a piece of
> code that would fail because memory barriers are missing.
>
> I wrote the following test, and it succeeds on PowerPC SMP (dual IBM
> Cell system QS-20), which is supposed to have weak memory ordering:
>
> http://svn.samalyse.com/misc/rbtest/test-int-array.c
>
> It is part of a (4 implementations) ring buffer test suite, that one
> can run with:
>
> svn co http://svn.samalyse.com/misc/rbtest
> cd rbtest
> make test
>
> Best regards,
>
> --
> Olivier Guilyardi / Samalyse
>
-----------------------------
Bjorn Roche
XO Wave
Digital Audio Production and Post-Production Software
http://www.xowave.com
http://blog.bjornroche.com
http://myspace.com/xowave