使用NSCondition解锁线程(目标C)

时间:2015-07-07 23:09:12

标签: objective-c iphone multithreading thread-safety nscondition

我是目标c的新手,并且正在尝试使用需要相互交谈的线程。

代码如下:

-(id) init
{
    self = [super init];
    if (self) {
        _event = [[MyTestClass alloc]init]; //MyTestClass has a property of type NSCondition
    }
    return self;
}


-(void) func1
{
NSLog(@"The Function 1 is being called");
NSLog(@"Locking The First function");
[self.event.myCondition lock];
[self.event.myCondition wait];
NSLog(@"Resuming Function 1");
    NSLog(@"This is a test 1 ");
    NSLog(@"This is a test 2");
    NSLog(@"Terminating func 1");
}
-(void) func2
{
    NSLog(@"2");
    NSLog(@"Hey Hey Hey How are you 0 ");
    NSLog(@"Hey Hey Hey How are you 1 ");
    NSLog(@"Hey Hey Hey How are you 2");
    NSLog(@"Signaling function 1");
    [self.event.myCondition signal];
    NSLog(@"Hey Hey Hey How are you 3");
    NSLog(@"Terminating func 2");
}

我在两个单独的线程上运行func1和func2,如下所示

- (void)viewDidLoad {
    [super viewDidLoad];
    SuperTestClass * n = [[SuperTestClass alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    MyTestClass * m = [[MyTestClass alloc]init];
    dispatch_queue_t newQueue =     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_group_t newGroup = dispatch_group_create();
    dispatch_group_async(newGroup, newQueue, ^{
        [n func1];
    });
    dispatch_group_async(newGroup, newQueue, ^{
        [n func2];
    });
    dispatch_group_wait(newGroup, DISPATCH_TIME_FOREVER);
    NSLog(@"All process have terminated");
}

运行此代码时出现以下错误

2015-07-07 19:05:54.528 signalingInObjectiveC[31617:319892] 2
2015-07-07 19:05:54.528 signalingInObjectiveC[31617:319894] The Function 1 is being called
2015-07-07 19:05:54.529 signalingInObjectiveC[31617:319892] Hey Hey Hey How are you 0 
2015-07-07 19:05:54.529 signalingInObjectiveC[31617:319894] Locking The First function
2015-07-07 19:05:54.529 signalingInObjectiveC[31617:319892] Hey Hey Hey How are you 1 
2015-07-07 19:05:54.529 signalingInObjectiveC[31617:319892] Hey Hey Hey How are you 2 
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319892] Signaling function 1
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319892] Hey Hey Hey How are you 3 
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319894] Resuming Function 1
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319892] Terminating func 2
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319894] This is a test 1
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319894] This is a test 2
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319894] Terminating func 1
2015-07-07 19:05:54.530 signalingInObjectiveC[31617:319870] All process have terminated
2015-07-07 19:05:54.535 signalingInObjectiveC[31617:319870] *** -[NSCondition dealloc]: condition (<NSCondition: 0x7fa2aa517180> '(null)') deallocated while still in use
2015-07-07 19:05:54.535 signalingInObjectiveC[31617:319870] *** Break on _NSLockError() to debug.

我想知道我解锁的方式有什么不对。

1 个答案:

答案 0 :(得分:0)

您滥用NSCondition类。检查apple doc。 本质上,POSIX条件与互斥锁一起使用,该互斥锁保护条件测试的共享数据。你应该有类似的东西(其中P是共享状态的布尔谓词)

线程#1:

[cond lock];
while (!P(state)) {
   [cond wait];
}
// invariant: if you get here, P(state) is true
... Use the state... // it is protected by the lock
[cond unlock];

线程#2:

[cond lock];
... change the state ... // this potentially changes the value of P
[cond signal];  // or [cond broadcast]
[cond unlock];

线程#2修改共享状态(例如,在共享buf中存储消息)并通知线程#1。线程#1无法唤醒,直到线程#2放弃锁定。注意线程#1中的[cond wait]调用如何原子地解锁互斥锁并进入睡眠状态。循环是必要的,原因有两个:

  1. 你可以得到虚假的唤醒(无缘无故,所以不能保证P(状态)是真的。
  2. 您可以使用多个线程执行线程#1所描述的内容。如果线程#2只存放一个数据,那么只有一个执行消费的线程实际上可以获取一个数据项并且消耗它&#34;将使P(状态)无效,所以其他人将重新回到睡眠状态。
  3. 底线,您的代码不正确,因为您无法解锁条件并在锁定时将其销毁。您没有遵循使用POSIX条件的编码模式。