NSCondition(转)

NSConditionLock 继承类NSObject 遵从的协议NSLocking NSObject

NSConditionLock 类定义特定的、用户定义的条件

协议NSLocking 方法 lock unlock

//初始化一个NSConditionLock对象
- (id)initWithCondition:(NSInteger)condition

//返回一个Condition
- (NSInteger)condition

//获取和释放锁
1、– (BOOL)lockBeforeDate:(NSDate *)limit
//在指定时间前尝试获取锁,若成功则返回YES 否则返回NO
2、– (void)lockWhenCondition:(NSInteger)condition

//尝试获取锁。在加锁成功前接收对象的Condition必须与参数Condition 相同

3、– (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit
//同上,只是又加上了一个时间

4、– (BOOL)tryLock //尝试着获取锁
5、– (BOOL)tryLockWhenCondition:(NSInteger)condition
//如果接收对象的condition与给定的condition相等,则尝试获取锁
6、– (void)unlockWithCondition:(NSInteger)condition
//解锁并设置接收对象的condition


//访问方法
– setName:
– name


NSCondition 类 断言

用一个NSCondition类对象的步骤
1、Lock the condition object.
2、Test a boolean predicate. (This predicate is a boolean flag or other variable in your code that indicates whether it is safe to perform the task protected by the condition.)
3、If the boolean predicate is false, call the condition object’s wait or waitUntilDate: method to block the thread. Upon returning from these methods, go to step 2 to retest your boolean predicate. (Continue waiting and retesting the predicate until it is true.)
4、If the boolean predicate is true, perform the task.
5、Optionally update any predicates (or signal any conditions) affected by your task.
6、When your task is done, unlock the condition object.
英语不太好也就不翻译了,大概意思都能看懂


下面看一下方法
//使线程等待
1、- (void)wait //阻塞当前线程,直到收到信号
2、- (BOOL)waitUntilDate:(NSDate *)limit
//阻塞当前线程,直到收到信号或到达指定的时间


///发信号给阻塞的线程
1、- (void)signal //唤醒一个阻塞的线程
2、- (void)broadcast //唤醒所有阻塞的线程


///访问方法
– setName:

– name

posted @ 2016-01-21 10:53  溺水的小小鱼  阅读(213)  评论(0编辑  收藏  举报