IOS 线程锁的例子

A:资源锁
#import <GHUnitIOS/GHUnit.h> 
@interface WPGlobalTest : GHTestCase{
    int         _count;
    NSLock      *_lock;
}
@end


-(void)test3{
    if (_lock == nil) {
        _lock = [[NSLock alloc] init];
    }

    
    NSThread *t1 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; 
    [t1 setName:@"t1"];
    [t1 start];
    
    NSThread *t2 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; 
    [t2 setName:@"t2"];
    [t2 start];
    
    NSThread *t3 = [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease]; 
    [t3 setName:@"t3"];
    [t3 start];
    
}

-(void)run{
    NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init];
    while (1) {
        //[_lock lock];
        _count++;
        NSThread *currentThread = [NSThread currentThread];
        NSLog(@"current thread name -> %@, count -> %d", [currentThread name], _count);
        [NSThread sleepForTimeInterval:1];
        //[_lock unlock];
    }
    [aPool drain];
}



@end


没有加锁(注释[_lock lock]和[_lock unlock]),资源(_count)会起冲突。

2012-12-11 13:20:14.340 Tests[7526:14903] current thread name -> t3, count -> 10

2012-12-11 13:20:14.340 Tests[7526:14403] current thread name -> t2, count -> 11

2012-12-11 13:20:14.340 Tests[7526:14103] current thread name -> t1, count -> 10

2012-12-11 13:20:15.343 Tests[7526:14403] current thread name -> t2, count -> 13

2012-12-11 13:20:15.343 Tests[7526:14903] current thread name -> t3, count -> 12

2012-12-11 13:20:15.343 Tests[7526:14103] current thread name -> t1, count -> 12

2012-12-11 13:20:16.347 Tests[7526:14903] current thread name -> t3, count -> 15

2012-12-11 13:20:16.347 Tests[7526:14103] current thread name -> t1, count -> 14

2012-12-11 13:20:16.347 Tests[7526:14403] current thread name -> t2, count -> 14


加锁之后,资源不会冲突


2012-12-11 13:23:53.325 Tests[7639:14103] current thread name -> t1, count -> 1

2012-12-11 13:23:53.326 Tests[7639:10703] WPGlobalTest/test3 ✔ 0.00s

2012-12-11 13:23:54.327 Tests[7639:14403] current thread name -> t2, count -> 2

2012-12-11 13:23:55.329 Tests[7639:14b03] current thread name -> t3, count -> 3

2012-12-11 13:23:56.332 Tests[7639:14103] current thread name -> t1, count -> 4

2012-12-11 13:23:57.334 Tests[7639:14403] current thread name -> t2, count -> 5

2012-12-11 13:23:58.337 Tests[7639:14b03] current thread name -> t3, count -> 6

2012-12-11 13:23:59.339 Tests[7639:14103] current thread name -> t1, count -> 7

2012-12-11 13:24:00.340 Tests[7639:14403] current thread name -> t2, count -> 8

2012-12-11 13:24:01.343 Tests[7639:14b03] current thread name -> t3, count -> 9




B:条件锁例子


#import <GHUnitIOS/GHUnit.h> 

@interface WPGlobalTest : GHTestCase{
    int             _count;
    NSLock          *_lock;//资源锁
    NSCondition     *_condition;//条件锁
    BOOL            _isOK;
}
@end



-(void)test4{
    if (_condition == nil) {
        _condition = [[NSCondition alloc] init];
        _isOK = NO;
    }
    
    
    [NSThread detachNewThreadSelector:@selector(run1) toTarget:self withObject:nil];
    [NSThread sleepForTimeInterval:10];
    [NSThread detachNewThreadSelector:@selector(run2) toTarget:self withObject:nil];

}

-(void)run1{
    NSLog(@"Start Run1 ...");
    [_condition lock];//锁定
    
    while (!_isOK) {
        NSLog(@"Run1 waiting ... ");
        [_condition wait];
    }
    
    [_condition unlock];
    NSLog(@"Stop Run1 ...");
}

-(void)run2{
    
    NSLog(@"Start Run2 ...");
    
    [_condition lock];
    
    _isOK = YES;
    
    NSLog(@"Change Value ...");
    
    [_condition signal];
    
    NSLog(@"Signal Run2 ...");
    
    [_condition unlock];

    NSLog(@"Stop Run2 ...");
}


@end


结果:

2012-12-11 14:17:32.548 Tests[9044:14103] Start Run1 ...

2012-12-11 14:17:32.549 Tests[9044:14103] Run1 waiting ... 

2012-12-11 14:17:42.549 Tests[9044:12407] Start Run2 ...

2012-12-11 14:17:42.550 Tests[9044:12407] Change Value ...

2012-12-11 14:17:42.550 Tests[9044:10703] WPGlobalTest/test4 ✔ 10.00s

2012-12-11 14:17:42.552 Tests[9044:12407] Signal Run2 ...

2012-12-11 14:17:42.553 Tests[9044:14103] Stop Run1 ...

2012-12-11 14:17:42.553 Tests[9044:12407] Stop Run2 ...





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值