Re: how to prevent unwanted hoist
On May 20, 1:57 am, Denise Kleingeist
<denise.kleinge...@googlemail.com> wrote:
Hello Andrew!
On May 17, 3:14 pm, "andrew_n...@yahoo.com" <andrew_n...@yahoo.com>
wrote:
class AtomicCounter {
volatile long val;
The next revision of the C++ standard will provide support for
atomic operations. The exact details are still under discussion
and currently I can't come up with the exact notation how this
would look like.
In any case, declaring your variable to be volatile won't have
much of an effect with respect to multiple threads accessing it
simultanously. Neither current nor future compilers will honour
it in a form useful for multi-threading purposes. However, it
should already prevent the compiler from hoisting access to it
from the loop but since it doesn't introduce any kind for memory
fences, that's about it.
This is a good point.
It is not clear to me what the OP's algorithm is, but the presence of
an unbalanced synchronization primitive is highly suspect.
InterlockedIncrement only guarantees atomicity with respect to other
Interlocked* functions. A naked increment will ruin the point of
using Interlocked* in the first place. This principle is not specific
to Windows.
In other words, there is a model/mood for doing synchronization that
the compiler cannot provide, and must be conscientiously engineered by
the programmer, then, after that, the elements of synchronization in
the language and the library can be brought to bear upon the design.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]