Re: Should I use mutex in this context?
On Wed, 29 Oct 2008 14:33:03 +0200, "Pavel A." <pavel_a@NOfastmailNO.fm>
wrote:
Doug,
The InterlockedXXX functions themselves are declared with volatile,
for example:
LONG InterlockedExchange( IN OUT LONG volatile *Target, IN LONG Value );
so it seems no, you can't get rid of the volatile completely.
It's superfluous, though. You certainly do NOT have to declare YOUR
variables volatile to use them with InterlockedXXX. If you have 15 years of
MSDN, it's an interesting exercise to trace the evolution of the
InterlockedXXX function declarations and the examples that accompany them.
I did this a few years ago and posted on it, concluding that they arrived
at the current declarations to make broken, poor quality MT code (e.g. many
MSDN examples) kinda sorta continue to work without having to cast volatile
away, where volatile was added to serve as an unreliable band-aid. I was
particularly amused to see intermediate forms declared similarly to:
LONG InterlockedExchange( LONG* volatile Target, LONG Value );
That doesn't even help broken examples compile right. :)
Re. "missing InterlockedRead":
For some time I used InterlockedAdd(var, 0), InterlockedOr(var,0) etc.
for this purpose... Now my belief is that modifying functions
(InterlockedAdd, Increment, Exchange) do all what is needed to ensure
that successive read-only access is correct.
I haven't looked at those for this purpose, but I suppose you can use any
of them that return the original value without modifying it. I just looked
up InterlockedAdd, and the docs say its Itanium only. I believe the
InterlockedCompareExchange function I mentioned has ubiquitous support.
--
Doug Harrison
Visual C++ MVP