TL;DR: I need the Microsoft C (not C++) equivalent of C11's atomic_load
. Anyone know what the right function is?
I have some pretty standard code which uses atomics. Something like
do {
bar = atomic_load(&foo);
baz = some_stuff(bar);
} while (!atomic_compare_exchange_weak(&foo, &bar, baz));
I'm trying to figure out how to handle it with MSVC. The CAS is easy enough (InterlockedCompareExchange
), but atomic_load
is proving more troublesome.
Maybe I'm missing something, but the Synchronization Functions list on MSDN doesn't seem to have anything for a simple load. The only thing I can think of would be something like InterlockedOr(object, 0)
, which would generate a store for every load (not to mention a fence)…
As long as the variable is volatile I think it would be safe to just read the value, but if I do that Visual Studio's code analysis feature emits a bunch of C28112 warnings ("A variable (foo) which is accessed via an Interlocked function must always be accessed via an Interlocked function.").
If a simple read is really the right way to go I think I could silence those with something like
#define atomic_load(object) \
__pragma(warning(push)) \
__pragma(warning(disable:28112)) \
(*(object)) \
__pragma(warning(pop))
But the analyzer's insistence that I should always be using the Interlocked*
functions leads me to believe there must be a better way. If that's the case, what is it?
#define atomic_load(object) (*(object))
(which is what I've done for MSVC in the past), but the code analyzer thing is giving me pause. – Guttle