Here's mine:
Again, avoid it if you can. Simply using a CriticalSection with spin-count is probably just as fast as well.Code:/*------------------------------------------------------------------------------ InterlockedRead - Read a "stable" value in an Interlocked fashion. Returns the the true value of the pointer "at the time" this function returns. This is really only useful for "read with full memory barrier" semantics. ------------------------------------------------------------------------------*/ template<typename T> T InterlockedRead(T *psrc) { T val; for (;;) { val = *psrc; if ((T)InterlockedCompareExchange((LONG*)psrc, (LONG)val, (LONG)val) == val) break; }//if return val; }//InterlockedRead
K.I.S.S.
>> ....volatile is needed
I disagree. Under MSVC you'll be generating an interlocked operation that you might as well do yourself (or not at all if you can avoid it!). Under any other compiler - volatile and multi-threaded access to variables have nothing to do with each other. But let's start another thread if we're going to go at it on this one
gg