我正在使用Objective,并试图使用需要相互交谈的线程。
守则如下:
-(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.我想知道我解锁的方式出了什么问题。
发布于 2015-07-08 00:49:40
您滥用了NSCondition类。检查一下苹果文档。本质上,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。在线程#2放弃锁之前,线程#1不能唤醒。注意调用cond在线程#1中是如何原子地解锁互斥锁并进入休眠状态的。循环是必要的,有两个原因:
总之,您的代码是不正确的,因为您未能解锁该条件,并在锁定时销毁它。您没有遵循使用POSIX条件的编码模式。
https://stackoverflow.com/questions/31280931
复制相似问题