iphone - Unlocking a thread using NSCondition (Objective C) -
i new objective c , trying work threads need talk each other.
the code follows :
-(id) init { self = [super init]; if (self) { _event = [[mytestclass alloc]init]; //mytestclass has property of type nscondition } return self; } -(void) func1 { nslog(@"the function 1 being called"); nslog(@"locking first function"); [self.event.mycondition lock]; [self.event.mycondition wait]; nslog(@"resuming function 1"); nslog(@"this test 1 "); nslog(@"this test 2"); nslog(@"terminating func 1"); } -(void) func2 { nslog(@"2"); nslog(@"hey hey hey how 0 "); nslog(@"hey hey hey how 1 "); nslog(@"hey hey hey how 2"); nslog(@"signaling function 1"); [self.event.mycondition signal]; nslog(@"hey hey hey how 3"); nslog(@"terminating func 2"); }
i run func1 , func2 on 2 separate threads below
- (void)viewdidload { [super viewdidload]; supertestclass * n = [[supertestclass alloc]init]; // additional setup after loading view, typically 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"); }
i following error when run code
2015-07-07 19:05:54.528 signalinginobjectivec[31617:319892] 2 2015-07-07 19:05:54.528 signalinginobjectivec[31617:319894] function 1 being called 2015-07-07 19:05:54.529 signalinginobjectivec[31617:319892] hey hey hey how 0 2015-07-07 19:05:54.529 signalinginobjectivec[31617:319894] locking first function 2015-07-07 19:05:54.529 signalinginobjectivec[31617:319892] hey hey hey how 1 2015-07-07 19:05:54.529 signalinginobjectivec[31617:319892] hey hey hey how 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 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] test 1 2015-07-07 19:05:54.530 signalinginobjectivec[31617:319894] 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] 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() debug.
im wondering whats wrong in way unlocking.
you misusing nscondition class. check apple doc. in essence, posix condition works mutex protects shared data condition tests. supposed have (where p boolean predicate on shared state)
thread #1:
[cond lock]; while (!p(state)) { [cond wait]; } // invariant: if here, p(state) true ... use state... // protected lock [cond unlock];
thread #2:
[cond lock]; ... change state ... // potentially changes value of p [cond signal]; // or [cond broadcast] [cond unlock];
thread #2 modifies shared state (e.g., deposits message in shared buf) , notifies thread #1. thread #1 cannot wake until thread #2 relinquishes lock. note how call [cond wait] in thread #1 atomically unlocks mutex , falls asleep. loop necessary 2 reasons:
- you can spurious wakeup (for no reason, there no guarantee p(state) true.
- you have several threads doing described thread #1. if thread #2 deposits 1 piece of data, 1 of threads doing consumption can pick data item , "consuming it" invalidate p(state), others go right sleep.
bottom line, code incorrect because fail unlock condition , destroy while locked. didn't follow coding pattern using posix condition.
Comments
Post a Comment