Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Last active December 24, 2015 21:59
Show Gist options
  • Save odrobnik/6869306 to your computer and use it in GitHub Desktop.
Save odrobnik/6869306 to your computer and use it in GitHub Desktop.
Using NSConditionLock - is this correct?
@implementation DTDrawingLock
{
NSUInteger _numberActiveOperations;
NSConditionLock *_lock;
}
- (instancetype)init
{
self = [super init];
if (self)
{
_lock = [[NSConditionLock alloc] initWithCondition:0];
}
return self;
}
- (void)pushDrawingOperation
{
[_lock lock];
_numberActiveOperations++;
[_lock unlockWithCondition:_numberActiveOperations];
}
- (void)popDrawingOperations
{
[_lock lock];
_numberActiveOperations--;
[_lock unlockWithCondition:_numberActiveOperations];
}
- (void)performLockProtectedOperation:(void(^)(void))operation
{
[_lock lockWhenCondition:0];
operation();
[_lock unlockWithCondition:0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment