Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Joseph_S_Intel
Employee
78 Views

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 (-=)
0 Kudos
1 Reply
jimdempseyatthecove
Black Belt
78 Views

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
Reply