missing operators for atomic<double> and atomic<float>

missing operators for atomic<double> and atomic<float>

I would like to do the following:

typedef atomic atomic_double;
atomic_double ad;
ad += 1;

However there isn't a '+=' operator for atomic (or atomiceither) but the operator exists for atomic. Is this an oversite? I have to go through and change a lot of code to switch from just 'double' to atomic becuase += is used a lot.

Also missing is fetch_and_add and fetch_and_decrement (-=)

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This is (probably) an oversight it should be relatively easy for you to extend the class

pseudo code

double AtomicAdd(double* pd, double d)
{
union {
volatile int64_t iOld;
double dOld;
};
union {
int64_t iNew;
double dNew;
};
for(;;)
{
iOld = *(int64_t*)pd;
dNew = dOld + d;
if(DCAS((int64_t*)pd, iOld, iNew))
return dNew;
}
}

On Linux for DCAS use

if(__sync_val_compare_and_swap((int64_t*)pd, iOld, iNew) == iOld)

On Windows you may need to use inline ASM to perform "LOCK CMPXCHG8B [reg]" if 64-bit intrinsic not available.

Jim Dempsey

Leave a Comment

Please sign in to add a comment. Not a member? Join today