[Interest] Heavily Commented Example: Simple Single Frontend with Two Backends
Till Oliver Knoll
till.oliver.knoll at gmail.com
Tue Oct 23 14:38:02 CEST 2012
2012/10/23 Graeme Gill <graeme2 at argyllcms.com>:
> 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".
> I doubt there is any luck involved. Coherent memory architectures
> are the simplest and least error prone for programmers used to
> single processor architectures, hence their popularity
> on hardware that supports general purpose operating systems
> and general purpose applications.
Still: using an QAtomicInt instead of a "volatile bool" is the same
effort (programming-wise), is about the same cost (performance-wise)
and makes your application also run on the other X% of
"non-cache-coherent" systems. And in case you also want to target
embedded/mobile platforms, it is not /that/ unlikely anymore that your
code might have to work on ARM etc. CPUs ;)
I understand that in the "terminate flag" use case (assuming again
cache-coherency) the worst which might happen is that the "worker"
thread does "one loop too much", in case the Core decides to re-order
the instructions such that you just miss the fact that the "continue"
flag has been set to false by the other Core. But eventually the
"worker" thread *will* see that the flag has been set to false (we
already agreed that when you have non-coherent caches then that
approach will fail here, too).
However when you rely on "volatile" for more delicate synchronisation
issues (other than this "terminate" flag use case) you might have a
hard time to debug - even on cache-coherent systems!
So I'd say, as a "rule of thumb", use QAtomicInt (instead of "volatile
bool") - or protect the data with mutexes (which will make sure that
memory is consistent).
More information about the Interest