I have some code like below :
so according to the code ,threadThree is supposed to run after both threadOne and threadTwo finished .
But the log told me that some time threadThree began to execute after _threadCount ++ and before the [myCondition signal] in either threadOne or threadTwo ,when _theadCount became 0 . Is it that threadThree will notice the _threadCount value changed when [myCondition signal] called ??
Hope someone can help me out .
I also post the project code to give more info.
Thanks a lot!
Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//
_myCondition = nil;
//
//
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30
target:self
selector:@selector(threadTester)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void)threadTester{
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(_myCondition){
[_myCondition release];
_myCondition = [[NSCondition alloc] init];
}
[_myCondition lock];
_threadCount = -2;
[_myCondition unlock];
//
NSLog(@"");
NSLog(@"------------------------------------------------------------------------------");
[NSThread detachNewThreadSelector:@selector(threadOne) toTarget:self withObject:nil];
[NSThread detachNewThreadSelector:@selector(threadTwo) toTarget:self withObject:nil];
[NSThread detachNewThreadSelector:@selector(threadThree) toTarget:self withObject:nil];
//
//[pool release];
return;
}
- (void)threadOne{
NSLog(@"@@@ In thread one start.");
[_myCondition lock];
int n = rand()%5 + 1;
NSLog(@"@@@ Thread one Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
sleep(n);
_threadCount ++ ;
NSLog(@"@@@ Thread one has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
[_myCondition signal];
NSLog(@"@@@ Thread one has signaled ,now _threadCount is : %d",_threadCount);
[_myCondition unlock];
NSLog(@"@@@ In thread one complete.");
[NSThread exit];
//_threadCount ++ ;
return;
}
- (void)threadTwo{
NSLog(@"### In thread two start.");
[_myCondition lock];
int n = rand()%5 + 1;
NSLog(@"### Thread two Will sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
sleep(n);
_threadCount ++ ;
NSLog(@"### Thread two has sleep %d seconds ,now _threadCount is : %d",n,_threadCount);
[_myCondition signal];
NSLog(@"### Thread two has signaled ,now _threadCount is : %d",_threadCount);
[_myCondition unlock];
NSLog(@"### In thread two complete.");
[NSThread exit];
return;
}
- (void)threadThree{
NSLog(@"<<< In thread three start.");
[_myCondition lock];
while (_threadCount < 0) {
[_myCondition wait];
}
NSLog(@"<<< In thread three ,_myCondition now is %d ,will start work.",_myCondition);
[_myCondition unlock];
NSLog(@"<<< In thread three complete.");
[NSThread exit];
return;
}
so according to the code ,threadThree is supposed to run after both threadOne and threadTwo finished .
But the log told me that some time threadThree began to execute after _threadCount ++ and before the [myCondition signal] in either threadOne or threadTwo ,when _theadCount became 0 . Is it that threadThree will notice the _threadCount value changed when [myCondition signal] called ??
Hope someone can help me out .
I also post the project code to give more info.
Thanks a lot!