GCD死锁情况

作者: 钟环 | 来源:发表于2016-10-09 15:23 被阅读461次

    造成死锁的情况分析

    第一种是:主队列同步任务很容易造成死锁,因为主队列是在主线程空闲时才会调度队列中的任务在主线程上执行.如果主线程不是空闲状态,那么主队列中不论是同步任务还是异步任务都会死锁,永远不会执行.

    - (void)gcdDemo1 
           NSLog(@"begin %@", [NSThreadcurrentThread]); 
          //  主队列同步执行任务  
           dispatch_sync(dispatch_get_main_queue(), ^{
                  NSLog(@"%@", [NSThreadcurrentThread]);
            });   
            NSLog(@"end %@", [NSThreadcurrentThread]);
    }
    
    - (void)gcdDemo2 {
        // 1. 串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.zh.queue", DISPATCH_QUEUE_SERIAL);
        NSLog(@"begin %@", [NSThreadcurrentThread]);
        //  主队列同步执行任务通过外层嵌套子线程保证当前函数中任务不需要等待主队列任务执行完.
        dispatch_async(queue, ^{ 
            dispatch_sync(dispatch_get_main_queue(), ^{ 
              NSLog(@"1%@", [NSThreadcurrentThread]);  
             });   
        });  
      //  由于主队列中放置了同步任务,通过上一步操作,表明主线程的任务必须要执行完才能执行下面的同步任务(也是主线程),但是下面的同步任务因为上面的主队列同步任务需要等待主线程空闲才能执行,相互等待,造成死锁.
        dispatch_sync(queue, ^{ 
            NSLog(@"2%@", [NSThreadcurrentThread]);
        });
        NSLog(@"end %@", [NSThreadcurrentThread]); 
    }
    

    注意:如果上面的:

       dispatch_sync(queue, ^{ 
            NSLog(@"2%@", [NSThreadcurrentThread]);
        });
    

    改成:

     dispatch_async(queue, ^{ 
            NSLog(@"2%@", [NSThreadcurrentThread]);
        });
    

    执行结果肯定永远是这个顺序:

    begin <NSThread: 0x7f89da4080d0>{number = 1, name = main}
    end <NSThread: 0x7f89da4080d0>{number = 1, name = main}
    1<NSThread: 0x7f89da4080d0>{number = 1, name = main}
    2<NSThread: 0x7f89da710f20>{number = 2, name = (null)}

    因为:上面的主队列调度主线程的是同步任务,需要立即执行完才能执行下面的操作,但是主线程目前还没有空闲,需要执行完本身gcdDemo2的NSLog(@"end %@", [NSThreadcurrentThread])才算完,所以begin —> end —>1 —>2


    第二种是:

    • 串行队列同步任务—>嵌套同步任务—>导致当前线程死锁.因为CPU执行到3的位置时,因为是同步任务,需要等待当前线程(目前是主线程)执行完,也即是2位置的外层嵌套的同步任务.由于外层嵌套的同步任务又要等待3位置的同步任务执行完,所以导致相互等待,死锁.
    - (void)gcdDemo4 {
    
        // 1. 串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.zh.queue", DISPATCH_QUEUE_SERIAL);
    
        // 2. 串行队列异步
        dispatch_sync(queue, ^{
            NSLog(@"begin %@", [NSThread currentThread]);
    
         // 3. 同步任务中嵌套同步任务会导致当前主线程死锁
         dispatch_sync(queue, ^{  
         NSLog(@"%@", [NSThread currentThread]);
        });
        NSLog(@"end %@", [NSThread currentThread]);   
     });
    }
    
    • 串行队列异步任务—>嵌套同步任务—>导致当前线程死锁.因为CPU执行到3的位置时,因为是同步任务,需要等待当前线程(目前是子线程)执行完,也即是2位置的外层嵌套的子线程的异步任务.由于外层嵌套的子线程异步任务执行时又要等待3位置的同步任务执行完,所以导致相互等待,死锁.
    - (void)gcdDemo4 {
        // 1. 串行队列
        dispatch_queue_t queue = dispatch_queue_create("cn.zh.queue", DISPATCH_QUEUE_SERIAL);
        // 2. 串行队列异步
        dispatch_async(queue, ^{
            NSLog(@"begin %@", [NSThread currentThread]);
        // 3. 异步任务中嵌套同步任务会导致当前子线程死锁
            dispatch_sync(queue, ^{ 
            NSLog(@"%@", [NSThread currentThread]);
            });
        NSLog(@"end %@", [NSThread currentThread]);   
      });
    }
    

    注意:如果换成并行队列就不会死锁,因为并行队列异步任务时,分配了不同的子线程,不会在一条子线程上死锁.


    总结:导致线程死锁情况只需要考虑两点:1)是不是同一个线程上.2)是不是相互等待

    容易导致死锁的情况:
    1)主队列同步任务,如果主线程上的执行函数没有用子线程去”包”一下,就会因为主线程上的执行函数BLOCK大任务与主队列中需要调度到主线程的BLOCK任务相互等待,导致死锁;

    注意:主队列只有在主线程空闲状态才会将任务调度到主线程执行,所以主队列的异步任务,也可能出现永远无法执行的情况.

    2)串行队列同步任务嵌套同步任务,如上演示的demo,会导致当前线程死锁;
    串行队列异步任务嵌套同步任务,如上演示的demo,会导致当前子线程死锁;

    相关文章

      网友评论

      • fbf8d408f08a:串行队列同步任务嵌套异步任务,会如何?
        钟环:@JimmyCheng 不会死锁。
      • d4a365850941:谢谢分享,很有用。
        钟环:@环敏 小敏敏说滴是。

      本文标题:GCD死锁情况

      本文链接:https://www.haomeiwen.com/subject/fzgpyttx.html