主要内容
- NSLock系
- @synchronized
- dispatch_semaphore_t
NSLock系
NSLock
NSLock *lock = [NSLock new];
for(NSInteger i = 0; i < 3; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[lock lock];
NSLog(@"%ld", i);
[NSThread sleepForTimeInterval:1];
[lock unlock];
});
}
结果(注意时间)
19:28:12.846 ObjectiveDemo[1146:728046] 0
19:28:13.850 ObjectiveDemo[1146:728047] 1
19:28:14.853 ObjectiveDemo[1146:728048] 2
NSLock还提供了tryLock和lockBeforeDate两个方法
- tryLock 尝试加锁,如果锁已经被锁住,不会阻塞线程,并返回NO
- lockBeforeDate 方法会在所指定Date之前尝试加锁,如果在指定时间之前都不能加锁,则返回NO
NSLock *lock = [NSLock new];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[lock lock];
NSLog(@"1");
[NSThread sleepForTimeInterval:2.0];
[lock unlock];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if([lock lockBeforeDate:[NSDate dateWithTimeIntervalSinceNow:10.0]])
{
NSLog(@"lock Success");
[lock unlock]
}
else
{
NSLog(@"lock Failure");
}
});
结果(注意时间)
19:36:37.409 ObjectiveDemo[1161:732625] 1
19:36:39.413 ObjectiveDemo[1161:732632] lock Success
NSConditionLock(条件锁)
NSConditionLock *lock = [[NSConditionLock alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[lock lockWhenCondition:12];
NSLog(@"1");
[lock unlock];
});
[NSThread sleepForTimeInterval:1.0];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[lock lock];
NSLog(@"2");
[lock unlockWithCondition:12];
});
结果
09:17:10.458 ObjectiveDemo[659:9989] 2
09:17:10.458 ObjectiveDemo[659:9993] 1
解释:第一个线程进入时锁住,设置条件为12,第二个线程运行时锁住,没有条件也没有其它的锁在执行,所以就开始执行,完了解开12的锁,第一线程那里就打开了
注意
- 使用initWithCondition初始化锁时这时这把锁是在当前条件下是打开的,默认初始条件为0
- NSConditionLock的实例同一时刻最多只有一个条件可以解锁
消费者和生产者模型
NSConditionLock *lock = [[NSConditionLock alloc] init];
NSMutableArray *produce = [NSMutableArray array];
//消费者
void(^consumer)(NSInteger) = ^(NSInteger condition){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[lock lockWhenCondition:condition];
NSLog(@"consumer: %@", produce.firstObject);
[produce removeObjectAtIndex:0];
[lock unlock];
});
};
//生成者
void(^producer)(NSInteger num) = ^(NSInteger num){
for(NSInteger i = 1; i <= num; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[lock lock];
[produce addObject:@(i)];
NSLog(@"producer: %ld", i);
[NSThread sleepForTimeInterval:1];
[lock unlockWithCondition:i];
});
}
};
consumer(1);
consumer(2);
consumer(3);
producer(3);
结果
09:30:50.745 ObjectiveDemo[731:18789] producer: 1
09:30:51.750 ObjectiveDemo[731:18780] consumer: 1
09:30:51.751 ObjectiveDemo[731:18813] producer: 2
09:30:52.754 ObjectiveDemo[731:18812] consumer: 2
09:30:52.755 ObjectiveDemo[731:18886] producer: 3
09:30:53.760 ObjectiveDemo[731:18814] consumer: 3
NSCondition 断言
在使用条件锁实现生产者和消费者关系时由于需要不断修改关系所以代码比较难看,NSCondition可以优雅的实现
NSCondition *condition = [[NSCondition alloc] init];
NSMutableArray *produce = [NSMutableArray array];
//消费者
void(^consumer)(void) = ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[condition lock];
while(produce.count == 0)
{
[condition wait];
}
NSLog(@"consumer: %@", produce.firstObject);
[produce removeObjectAtIndex:0];
[condition unlock];
});
};
//生成者
void(^producer)(NSInteger num) = ^(NSInteger num){
for(NSInteger i = 1; i <= num; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[condition lock];
[produce addObject:@(i)];
NSLog(@"producer: %ld", i);
[condition signal];
[NSThread sleepForTimeInterval:1];
[condition unlock];
});
}
};
consumer();
consumer();
consumer();
producer(3);
结果
09:45:56.659 ObjectiveDemo[802:28917] producer: 1
09:45:57.660 ObjectiveDemo[802:28825] producer: 2
09:45:58.664 ObjectiveDemo[802:28918] producer: 3
09:45:59.669 ObjectiveDemo[802:28852] consumer: 1
09:45:59.669 ObjectiveDemo[802:28853] consumer: 2
09:45:59.670 ObjectiveDemo[802:28817] consumer: 3
注意:signal一次只能唤醒一个wait,如果有多个wait可以使用broadcast
1.4 NSRecursiveLock 递归锁
在有需求实现递归调用,但是实现部分又得加锁,如果使用普通锁,就会造成死锁,比如下面
- (void)recursiveCount:(NSInteger)count
{
static NSLock *lock;
lock = lock ? lock : [NSLock new];
[lock lock];
if(count)
{
NSLog(@"start:%ld", count);
[NSThread sleepForTimeInterval:1];
[self recursiveCount:count - 1];
NSLog(@"end:%ld", count);
}
[lock unlock];
}
解释:end是无法输出的,因为在他之前又递归调用了,到了锁的时候发现上一个锁还没完成然后就开始等待,使用递归锁可以解决
- (void)recursiveCount:(NSInteger)count
{
static NSRecursiveLock *lock;
lock = lock ? lock : [NSRecursiveLock new];
[lock lock];
if(count)
{
NSLog(@"start:%ld", count);
[NSThread sleepForTimeInterval:1];
[self recursiveCount:count - 1];
NSLog(@"end:%ld", count);
}
[lock unlock];
}
结果
10:25:29.171 ObjectiveDemo[884:46332] start:2
10:25:30.173 ObjectiveDemo[884:46332] start:1
10:25:31.174 ObjectiveDemo[884:46332] end:1
10:25:31.175 ObjectiveDemo[884:46332] end:2
@synchronized
这是一种非常简洁而且安全的加锁方式,内部自动判断了当前使用加锁的方式(递归锁的判断)
- (void)recursiveCount:(NSInteger)count
{
@synchronized (self) {
if(count)
{
NSLog(@"start:%ld", count);
[NSThread sleepForTimeInterval:1];
[self recursiveCount:count - 1];
NSLog(@"end:%ld", count);
}
}
}
注意:@synchronized (obj)只是对同一个obj进行互斥
dispatch_semaphore_t
这是通过GCD的信号来加锁的,在我的GCD中已经讲过,可以去那了解一下,不怎么建议使用这种方式加锁。因为这是实现并发用的,只是把并发数设置为1,就有了锁的效果了