Atomic load in C with MSVC

TL DR: I need the C11 equivalent of atomic_loadMicrosoft C (not C ++). Does anyone know what the correct function is?

I have a pretty standard code that uses atoms. Sort of

do {
  bar = atomic_load(&foo);
  baz = some_stuff(bar);
} while (!atomic_compare_exchange_weak(&foo, &bar, baz));

I am trying to figure out how to handle MSVC. CAS is fairly simple ( InterlockedCompareExchange), but atomic_loadit turns out to be more unpleasant.

Maybe something is missing for me, but the list of synchronization functions in MSDN does not seem to have anything for a simple download. The only thing I can think of is something like InterlockedOr(object, 0)that would create a repository for each download (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 this Visual Studio code analysis will highlight a bunch of C28112 warnings ("the variable (foo) that is accessed through the lock function should always be accessible through the function blocking. ").

If simple reading is really the right way, I think I can silence those who have 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 use functions Interlocked*makes me think that there should be a better way. If so, what is it?

+4
source share
1 answer

, , , (32- 32 , 64- 64- ). , .

, , idempotent Interlocked . , :

#define atomic_load(object) InterlockedOr((object), 0)

0 , , .

atomic_load_explicit memory_order_relaxed, , InterlockedOrNoFence, , ( ) atomic_load, InterlockedOr.

InterlockedOr ( , , , ), InterlockedXor 0 , , .

InterlockedCompareExchange ; , , :

#define atomic_load(object) InterlockedCompareExchange((object), 0, 0)

, 0, , , , , no-op.

+1

Source: https://habr.com/ru/post/1671756/


All Articles