0
-(void)test1{
    dispatch_queue_t queue = dispatch_get_main_queue();
    NSLog(@"start");
    dispatch_sync(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
    });
}

-(void)test2{
    dispatch_queue_t queue = dispatch_queue_create("com.yaoye.serial", DISPATCH_QUEUE_SERIAL);
    NSLog(@"start");
    dispatch_sync(queue, ^{
        NSLog(@"%@",[NSThread currentThread]);
    });
}

Test1 and test2 are executed in the main thread

Test1 example:

the main thread is blocked waiting for synchronization function, block into the main thread of the runloop cannot be executed, lead to deadlock.

Test2 example:

the main thread waiting for synchronization function is blocked,block into the main thread of the runloop, but no deadlock.<2016-03-14 13:55:06.730 GCD[54320:12111593] <NSThread: 0x7fef4ac08810>{number = 1, name = main}>

queation:

Why not is test2 deadlock?

Community
  • 1
  • 1
yaoye
  • 3
  • 2

1 Answers1

0

because test1 uses the same main queue, whereas test2 uses a different queue

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161