DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C Concurrency Less Big Fail

12.01.2011
| 2243 views |
  • submit to reddit
        
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;
}