3 ms·
This is called a semaphore, it's already implemented in GCD. Also, why talk about performance and then make obj-c method calls...? It's quite easy using NSPro
by fleitz 11y ago
This is called a semaphore, it's already implemented in GCD.
Also, why talk about performance and then make obj-c method calls...?
It's quite easy using NSProxy to create a throttler that will wrap any object, then you can abstract throttling from the behavior of the underlying object.
@interface Throttler : NSProxy {
dispatch_semaphore_t _semaphore;
id _object;
}
- initWithObject:(id)obj concurrentOperations:(int)ops;
@end
@implementation Throttler
- (id) initWithObject:(id)obj concurrentOperations:(int)ops {
if(self = [super init]){
_semaphore = dispatch_semaphore_create(ops)
_object = obj;
}
return self;
}
- (void) forwardInvocation:(NSInvocation*)invocation {
if(dispatch_semaphore_wait(_semaphore,0)){
@try {
[invocation setTarget: _object];
[invocation invoke];
}
@catch (NSException* e){
@throw e;
}
@finally {
dispatch_semaphore_signal(_semaphore);
}
return;
}
@throw [NSException
exceptionWithName:@"InsufficientResourceException"
reason:@"Insufficient Resource"
userInfo:nil];
}
@end
https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW24 https://developer.apple.com/library/mac/documentation/Genera...