objective c - GCD deadlock on OS X but not on iOS -


i have code in viewdidload

- (void)viewdidload {     [super viewdidload];     dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0);     nslog(@"first");     dispatch_async(queue, ^{         nslog(@"third");         dispatch_sync(queue, ^{             nslog(@"fourth");         });         nslog(@"last");     });     nslog(@"second"); } 

which prints out:

2015-07-20 19:12:54.325 test[7574:431705] first 2015-07-20 19:12:54.326 test[7574:431736] third 2015-07-20 19:12:54.326 test[7574:431736] fourth 2015-07-20 19:12:54.326 test[7574:431705] second 2015-07-20 19:12:54.326 test[7574:431736] last 

this differs on executions. prints "first, second, third, fourth, last" , other times produces the output above.

also, have test.m file following: #include

int main() {   dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0);   nslog(@"first");   dispatch_async(queue, ^{       nslog(@"third");       dispatch_sync(queue, ^{           nslog(@"fourth");         });       nslog(@"last");     });   nslog(@"second");   return 0; } 

and produces:

2015-07-20 19:12:36.067 tester[7568:431388] first 2015-07-20 19:12:36.068 tester[7568:431388] second 

i don't understand why on ios simulator prints out of logs while on mac prints "first" , "second" deadlocks. expected ios code deadlock instead prints out "third" , "fourth". there isn't other deals threading in ios code. when nslog(@"first") commented out, on executions of test.m produces different shown below:

$: ./tester 2015-07-20 19:19:31.030 tester[7608:433729] second $: ./tester 2015-07-20 19:19:32.268 tester[7609:433738] third 2015-07-20 19:19:32.268 tester[7609:433737] second $ ./tester 2015-07-20 19:19:32.620 tester[7610:433740] second $: ./tester 2015-07-20 19:19:33.812 tester[7611:433744] third 2015-07-20 19:19:33.812 tester[7611:433743] second 

i have no idea causing difference in output. way ios handles threading differs os x? appreciate if explain going on

on mac not have deadlock. simple case of main completing , app terminating before background thread has chance start or complete.

keep in mind order of seeing "second" , "third" in log undefined. time dispatch_async returns, have 2 concurrent threads nslog on main queue , background queue output in either order.

if block main thread (such using sleep) before return statement, should see logs both threads.


Comments