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

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


2012/10/23 Thomas McGuire <thomas.mcguire at kdab.com>:
> > ...
> If you share data between threads, you need to protected them by a mutex or
> use aquire/release barriers manually. A mutex has implicit aquire/release
> semantics.

THAT'S exactly the point I was missing here! Thanks for the excellent
clarification! I wasn't aware that QMutex would also make sure "under
the hood" that memory / caches were synchronised, too, before granting
access to the Critical Section! :)

And yes, declaring a variable as "volatile" generates code such that
the variable is not placed into a register, but into ordinary RAM
instead (I guess - unfortunately I never took a "compiler class", and
my steps in Assembler were very few back in those days ;)) But off
course the CPU will (might) still cache it into 1st or 2nd level
cache.

As Bo pointed out on "ordinary desktops" you are probably lucky, since
the Intel desktop architecture (single CPU, multiple cores) apparently
have "coherent caches".

But I do get the point now that you should

- Use "QAtomicInt" instead of a "volatile bool" (for the simple "Stop
thread" use case) or
- Protect access with QMutex

in order to make sure that memory is synchronised across cache (or
even dedicated RAM) boundaries.

> ...
> Using a mutex is the best first choice: It protects your data against
> concurrent access and makes sure the data updates is visible on all CPUs using
> memory barriers.

Yes, that is now understood :) I wasn't aware of the "memory barrier" aspect!

Thanks again!
  Oliver



More information about the Interest mailing list