If we have:
__int32 some_var = 0;
What is the best (if any) way to call InterlockedExchange
, InterlockedIncrement
and other interlocked functions which require LONG*
for some_var
?
Since, there is guarantee that LONG
is 32 bit on any Windows, it's probably safe just to pass (long*) some_var
. However, it seems to me quite ugly and I can't find confirmation that it's safe.
Note, I can't change type to long
because it's not portable. I need exactly 32 bit type.
Update: some research of libraries which provide portable atomic operations has shown that no one bothers about casting. Some examples:
Apache Portable Runtime (APR):
typedef WINBASEAPI apr_uint32_t (WINAPI * apr_atomic_win32_ptr_val_fn)
(apr_uint32_t volatile *,
apr_uint32_t);
APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
#if (defined(_M_IA64) || defined(_M_AMD64))
return InterlockedExchangeAdd(mem, val);
#elif defined(__MINGW32__)
return InterlockedExchangeAdd((long *)mem, val);
#else
return ((apr_atomic_win32_ptr_val_fn)InterlockedExchangeAdd)(mem, val);
#endif
}
AO_INLINE AO_t
AO_fetch_and_sub1_full (volatile AO_t *p)
{
return _InterlockedDecrement64((LONGLONG volatile *)p) + 1;
}