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

Bo Thorsen bo at fioniasoftware.dk
Mon Oct 22 16:24:53 CEST 2012

Den 22-10-2012 15:51, Jan Kundrát skrev:
> On 10/22/12 14:16, Bo Thorsen wrote:
>> Not correct. Neither the compiler nor the CPU can reorder around a volatile.
> Hi Bo,
> various sources which I've read about this topic (like [1], [2], [3],
> [4]) disagree. I might be wrong and would like to be corrected in that
> case -- references are welcome.

This is getting a bit hairy and probably way too hard for this mailing 
list. First, you and Thomas are correct that the order of writes is 
impossible to control. For a stop flag on a thread, this is fine, as 
long as only one thread is writing to it - this is the case in most code 
I've seen.

On x86 processors, the cores are cache coherent. If you're on an 
architecture where this isn't the case, volatile is indeed useless.

If you're on a C++11 compiler, you should use one of the new atomic types.

Unless you absolutely have to stop a thread loop immediately, volatile 
is perfectly fine for a stop flag and there's no reason to use a bigger 
hammer on such a small nail. For any real data, or data written to by 
more than one thread, volatile is useless and you need to use some type 
of protected region.

This is exactly the case that Syam asked about.

Bo Thorsen.

Come by my DevDays talk in Berlin - "Designing for testability". Learn how to build and run your unit tests with the Qt application.

Fionia Software - Qt experts for hire.

More information about the Interest mailing list