0
int main(void) {
    dispatch_queue_t queue = dispatch_queue_create(“com.somecompany.queue”, nil);
    dispatch_sync(queue, ^{ // task 1
        NSLog(@"Situation 1"); 
    });
    return 0;
}

This is OK run in the main().

//-------------------------------------------

int main(void) {
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{ // task 1
        NSLog(@"Situation 2"); 
    });
    return 0;
}

This is DEAD-LOCK in the main().

//-------------------------------------------

Why situation 1 is OK while situation 2 is DEAD-LOCK ? Both are sync call serial queue in main thread.

Or just because sync() itself run in the main queue ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
abyn
  • 1
  • 1

1 Answers1

0

In the first case you block the main queue until the task has finished executing on the dispatch queue you created, so there is no problem.

In the second case you are trying to dispatch your task on the main queue, but the main queue is blocked by the dispatch_sync, so the submitted closure can't start. The result is a deadlock

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Is that to say, `int main(void) {NSLog(@"Task 1"); return 0;}`, the `NSLog()` is run on the main queue ? Or all the functions run in the `main()` are run on the main queue ? – abyn Dec 25 '16 at 04:49
  • Everything in your program runs on the main queue except for code that you explicitly dispatch onto another queue. Code on the main queue will always run on the main thread. Code dispatched on another queue may or may not run on the main thread. – Paulw11 Dec 25 '16 at 06:15
  • Oh so that is what it is ! Thx to @Paulw11, now i know the things. – abyn Dec 25 '16 at 12:07