zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Windows Phone开发之路(10) 进军WP的第一个程序
    Windows Phone开发之路(9) Silverlight之动画
    Windows Phone开发之路(13) 触摸
    使用Beetle进行高效的自定义二进制序列化
    网络通讯合并数据发送的重要性和实现原理
    开源网络文件管理工具SmarkNetDisk
    .net socket吞吐能力测试结果
    数值压缩存储方法Varint
    .NET 程序优化不要仅仅盯着代码执行时间
    ActionScript简单实现Socket Tcp应用协议分析器
  • 原文地址:https://www.cnblogs.com/xiaoxiaoyublogs/p/5147535.html
Copyright © 2011-2022 走看看