iOS常见锁对象:
1.互斥锁 NSLock
NSLock * lock = [[NSLock alloc] init];
[lock lock];
//处理操作
[lock unlock];
2.对象锁 @synchronized(self)
static NSObject *obj = nil;
@synchronized(self)
{
if (obj == nil) {
obj = [[NSObject alloc] init];
}
return obj;
3.条件锁 NSConditionLock
//条件锁
NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:1];
//任务
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//加锁
[lock lockWhenCondition:1];
NSLog(@"-------任务1");
//解锁
[lock unlockWithCondition:2];
});
//任务
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//加锁
[lock lockWhenCondition:2];
NSLog(@"-------任务2");
//释放锁
[lock unlock];
});
4.递归锁 NSRecursiveLock