by lyinsteve · 11y ago · 0 comments
The most Cocoa-compatible way of handling background execution of expensive procedures is always going to be best executed, quickest, using Grand Central Dispatch.
For example:
@interface Foo ()
@property (nonatomic) dispatch_queue_t backgroundQueue;
@end
@implementation Foo
- (BOOL) veryExpensiveMethod:(id)arg completion:(void (^)())completion {
dispatch_async(self.backgroundQueue, ^{
if (_counter++ > N) {
_counter--;
return NO;
}
// Critical section
_counter--;
return YES;
dispatch_async(dispatch_get_main_queue(), completion);
};
That will ensure every call to -veryExpensiveMethod is run in sequence, and won't require waiting on your end.These problems have been solved, better.
If you had read 'til the end you would have found multiple statements that OSAtomic* is merely an alternative. Not a silver bullet. Not the fastest.
From the conclusion:
"It's very important to understand that every example in this article could have legitimately been solved with different concurrency primitives — like semaphores and locks — without any noticeable impact to a human playing around with your app."
Also, "(...) is always going to be best executed, quickest, using GCD." is kind of a blanket statement. I'd be careful around the use of "always".