- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 (-=)
typedef atomic
atomic_double ad;
ad += 1;
However there isn't a '+=' operator for atomic
Also missing is fetch_and_add and fetch_and_decrement (-=)
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page