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

Jan Kundrát jkt at flaska.net
Mon Oct 22 12:10:20 CEST 2012


On 10/22/12 07:21, d3fault wrote:
>     volatile bools work

I wasn't able to find a context for this, but in general, this is not 
true. Declaring a variable volatile does *not* make it suitable to be 
used as a synchronization primitive across threads. As a side effect, it 
might also make the code run slower.

According to my understanding, the reason for this is the C++ memory 
model (or lack thereof); while volatile might look like a great way to 
force an "always load stuff from memory" approach, it does not impose a 
particular ordering of the read/write operations, so the 
compiler/CPU/memory controller/... are actually allowed to reorder 
access to other memory regions (and they really do that). That's not 
what you want to do.

In general -- like cryptography, synchronization of multithreaded 
operations is hard, and it's best not to invent "creative" ways of doing 
that unless you are an expert. Use the high-level operations at first, 
and if you are attracted to tinkering with the low-level bits, read a 
good book before you do so. I can only recommend Anthony Williams' C++ 
Concurrency in Action.

This is not directed to d3fault in particular, it's a general advice 
about not messing with threads.

Cheers,
Jan

-- 
Trojita, a fast e-mail client -- http://trojita.flaska.net/



More information about the Interest mailing list