@interface NSConditionLock : NSObject <NSLocking> {
@private
void *_priv;
}
- (instancetype)initWithCondition:(NSInteger)condition NS_DESIGNATED_INITIALIZER;
@property (readonly) NSInteger condition;
- (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;
@property (nullable, copy) NSString *name NS_AVAILABLE(10_5, 2_0);
@end
NSConditionLock
和 NSLock
类似,都遵循 NSLocking
协议,方法都类似,只是多了一个 condition
属性,以及每个操作都多了一个关于 condition
属性的方法,例如 tryLock
,tryLockWhenCondition:
,NSConditionLock
可以称为条件锁,只有 condition
参数与初始化时候的 condition
相等,lock
才能正确进行加锁操作。而 unlockWithCondition:
并不是当 Condition
符合条件时才解锁,而是解锁之后,修改 Condition
的值。