[Interest] Heavily Commented Example: Simple Single Frontend with Two Backends

Till Oliver Knoll till.oliver.knoll at gmail.com
Tue Oct 23 12:48:31 CEST 2012


2012/10/23 Harri Pasanen <harri at mpaja.com>:
> On 10/23/2012 12:14 PM, Till Oliver Knoll wrote:
>> As Bo pointed out on "ordinary desktops" you are probably lucky, since
>> the Intel desktop architecture (single CPU, multiple cores) apparently
>> have "coherent caches".
>
> Just a note that things like out-of-order execution and threading behave
> subtly different when using multicore ARM chips compared to Intel.

Yes! As a matter of fact, even though on "Intel x86 desktop multicore"
architecture caches might be coherent, you would STILL have a problem
with instruction re-ordering, as done by a single core. So declaring a
"boolean flag" as volatile is definitively NOT the way to do it ;)

I recommend reading especially the chapter "Out-of-order execution
versus compiler reordering optimizations" here:

  http://en.wikipedia.org/wiki/Memory_barrier

Quote:

"The C and C++ standards do not address multiple threads (or multiple
processors),[citation needed] and as such, the usefulness of volatile
depends on the compiler and hardware. Although volatile guarantees
that the volatile reads and volatile writes will happen in the exact
order specified in the source code, the compiler may generate code (or
the CPU may re-order execution) such that a volatile read or write is
reordered with regard to non-volatile reads or writes, thus limiting
its usefulness as an inter-thread flag or mutex. [...]. Moreover, it
is not guaranteed that volatile reads and writes will be seen in the
same order by other processors or cores due to caching, cache
coherence protocol and relaxed memory ordering, meaning volatile
variables alone may not even work as inter-thread flags or mutexes.

Some languages and compilers may provide sufficient facilities to
implement functions which address both the compiler reordering and
machine reordering issues. In Java version 1.5 (also known as version
5), the volatile keyword is now guaranteed to prevent certain hardware
and compiler re-orderings, as part of the new Java Memory Model. C++11
standardizes special atomic types and operations with semantics
similar to those of volatile in the Java Memory Model."


I've learnt something important today :)

Cheers, Oliver



More information about the Interest mailing list