IOS 線程鎖的例子

來源:互聯網
上載者:User
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 ...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.