zoukankan      html  css  js  c++  java
  • NSConditionLock

    一、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

      

      

  • 相关阅读:
    VB.net和c#利用IOleCommandTarget接口实现Html编辑器的Glyphs(原创)
    vb.net中用com通讯实现对HTMLDocument的事件捕捉(原创)
    自己写的用JS脚本定时执行Web地址并且得到它的源代码的页面程序
    Alice:曾经的CS反恐精英顶尖高手
    .net利用IOleCommandTarget接口实现Html编辑器表格边框的显示和隐藏(原创)
    ASP用语法及函数
    最近在研究质量统计与分析方面的项目开发
    propertyGrid实现选项(原创)
    .Net平台AOP技术研究
    c# propertyGrid属性控件跟Htmlelement元素绑定(原创精华)
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/4673533.html
Copyright © 2011-2022 走看看