Over a million developers have joined DZone.

C Concurrency Less Big Fail

·

int64_t AddAndGetAtomic(Object *o, int64_t amount) {
    int64_t oldval;
    int64_t returnval;
    bool success = 0;
    do {
        oldval = o->counter;
        returnval = oldval + amount;
        success = OSAtomicCompareAndSwap64(oldval, returnval, &(o->counter);
    } while (success == 0);

    return returnval;
}
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}