IOS NSConditionLock 条件锁 NSCondition 断言

NSConditionLock  条件锁    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


自己看官方文档是的总结 —— LC


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值