Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2465 Discussions

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

Joseph_S_Intel
Employee
301 Views
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
Honored Contributor III
301 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
0 Kudos
Reply