一、NSConditionLock定义了一个可以指定条件的互斥锁,用于线程之间的互斥与同步。
这里的条件并不是bool表达式中的条件,而是一个特定的int值。
二、NSConditionLock的API
NSConditionLock实现了NSLocking协议,有lock和unlock方法。
NSConditionLock具有以下方法:
- (void)lockWhenCondition:(NSInteger)condition; - (BOOL)tryLock; - (BOOL)tryLockWhenCondition:(NSInteger)condition; - (void)unlockWithCondition:(NSInteger)condition; - (BOOL)lockBeforeDate:(NSDate *)limit; - (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit;
lockWhenCondition :用于condition等于特定值的时候加锁,会阻塞当前线程。
tryLock,tryLockWhenCondition :尝试加锁,加锁失败时返回NO,try开头的方法会理解解锁。
lockBeforeDate,lockWhenCondition:指定条件加锁,会阻塞当前线程。
unlockWithCondition:指定条件时解锁,每次解锁会导致内部的condition值改变为指定的值,同时唤醒其它阻塞的线程检测这里的condition是否满足条件,因此NSConditionLock相对于NSCondition效率更低。
三、demo
接上一节中的main方法,修改NSConditionSample:
@interface NSConditionSample ()
{
NSConditionLock *conditionLock;
NSMutableArray *products;
} @end @implementation NSConditionSample - (instancetype)init
{
if(self = [super init])
{
conditionLock = [[NSConditionLock alloc] initWithCondition:0];
products = [[NSMutableArray alloc] init];
}
return self;
} - (void)createConsumenr
{
while (YES) {
NSLog(@"createConsumenr before lock");
[conditionLock lockWhenCondition:10];
NSLog(@"createConsumenr after lock");
if([products count] > 0)
[products removeObjectAtIndex:0];
NSLog(@"comsume a product,left %d products",[products count]);
[conditionLock unlockWithCondition:[products count]==0?0:10];
}
} - (void)createProducter
{
while (YES) {
NSLog(@"createProducter before lock");
[conditionLock lock];
NSLog(@"createProducter after lock");
[products addObject:[[NSObject alloc] init]];
NSLog(@"produce a product,left %d products",[products count]);
[conditionLock unlockWithCondition:[products count]];
} } @end
执行结果:
2015-07-24 13:06:31.323 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.323 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.323 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.323 NSConditionSample[47237:41183762] produce a product,left 1 products
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] produce a product,left 2 products
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] produce a product,left 3 products
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] produce a product,left 4 products
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.324 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.325 NSConditionSample[47237:41183762] produce a product,left 5 products
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] produce a product,left 6 products
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] produce a product,left 7 products
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] produce a product,left 8 products
2015-07-24 13:06:31.336 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] produce a product,left 9 products
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183763] createConsumenr after lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183763] comsume a product,left 9 products
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.337 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] createConsumenr after lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] comsume a product,left 9 products
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] createConsumenr after lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] comsume a product,left 9 products
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.338 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.338 NSConditionSample[47237:41183763] createConsumenr after lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] comsume a product,left 9 products
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] createConsumenr after lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] comsume a product,left 9 products
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] createConsumenr before lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] createProducter after lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] produce a product,left 10 products
2015-07-24 13:06:31.339 NSConditionSample[47237:41183762] createProducter before lock
2015-07-24 13:06:31.339 NSConditionSample[47237:41183763] createConsumenr after lock