1
- (void)decrementActivityCount {
    [self willChangeValueForKey:@"activityCount"];
    bool success;
    do {
        int32_t currentCount = (int32_t)_activityCount;
        success = OSAtomicCompareAndSwap32(currentCount, MIN(currentCount - 1, currentCount), &_activityCount); 
        //Incompatible pointer types passing 'NSInteger *' (aka 'long *') to parameter of type 'volatile int32_t *' (aka 'volatile int *')
    } while(!success);
    [self didChangeValueForKey:@"activityCount"];
    [self updateNetworkActivityIndicatorVisibilityDelayed];
}

_activityCount is an NSInteger

Above is my code and two issues.

  1. What is the OSAtmonicCompareAndSwap32 actually doing?
  2. How can I remove the warning?
software is fun
  • 7,286
  • 18
  • 71
  • 129

1 Answers1

1

What is the OSAtmonicCompareAndSwap32 actually doing?

It atomically compares currentCount and _activityCount, and if they are equal, it sets _activityCount to MIN(currentCount - 1, currentCount).

It returns true if the comparison was equal.

So, basically it performs an atomic decrement by 1. The same can be accomplished with less code.

How can I remove the warning?

_activityCount MUST be an int32_t, OR

use 64-bit integers and the 64-bit variants of CompareAndSwap:

OSAtomicCompareAndSwap64

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67