私は Objective-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");
}
以下のように、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.
ロックを解除する方法に何が問題なのか疑問に思っています。