int total = 0; // these are globals.. BOOL dispatchCalled = NO; // -(void) callDispatch { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{ dispatchCalled = YES; NSLog(@"Total, after 300ms, is %i", total); }); } -(void)play // this is my "main" method.. { NSLog(@"app starts running"); [self callDispatch]; while(!dispatchCalled) { total++; } [self callDispatch]; }
Console
:
2012-08-02 20:36:05.357 MyProject[8245:1a07] app starts running 2012-08-02 20:36:05.693 MyProject[8245:3d03] Total, after 300ms, is 11513522 2012-08-02 20:36:05.993 MyProject[8245:3d03] Total, after 300ms, is 11513523
When the method enclosed in callDispatch is executed for the first time, the while-loop managed to execute 11513522 times. At this point, the while-loop condition is set to YES , and the while loop should run no more. However, it runs again before it confirms the update by the condition of the submit method. Why is this?
This is because the method enclosed in callDispatch will execute in parallel / in parallel with the while loop, which explains why the confirmation of another loop is needed for the while loop?
user1073400
source share