美文网首页IOS
dispatch_sync

dispatch_sync

作者: Crazy2015 | 来源:发表于2016-03-02 10:51 被阅读163次

    dispatch_async 函数的“async”意味着“非同步”(asynchronous),就是将指定的Block“非同步”地追加到指定的Dispatch Queue中。dispatch_async函数不做任何等待。

    屏幕快照 2016-03-01 下午10.16.31.png

    既然有“async”,当然就有“sync”,即 dispatch_sync函数。它意味着“同步”(synchronous),也就是将指定的Block" 同步"追加到指定的Dispatch Queue中。在追加Block结束之前,dispatch_sync 函数会一直等待。


    屏幕快照 2016-03-01 下午10.20.29.png
    dispatch_sync函数简单,容易引起问题。即死锁。

    例如如果在主线程中执行以下源代码就会死锁。

    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{NSLog(@"Hello")});
    

    也就是说,dispatch_sync函数在等待block块代码执行完毕才能执行,而block块的执行在主线程中,所以dispatch_sync在主线程中调用的话就会发生死锁。

    发生死锁有如下解释:

    在这里只考虑Serial Dispatch Queue,比如Main Thread Queue;而Concurrent Dispatch Queue一般情况不会发生,暂时没有研究。dispatch_sync(queue1, block);假设该语句在queue0中执行,则会阻塞(等待)queue0直到queue1中的block执行结束。当queue0和queue1不同时,queue0与queue1为并行执行,此时queue0等待,queue1执行block,queue0与queue1互不影响,直到queue1的block执行结束,queue0阻塞也结束,即dispatch_sync语句返回。当queue0和queue1相同(且为串行队列Serial Dispatch Queue)时,即dispatch_sync与block在同一个队列中,此时queue0已经阻塞,不可能再执行block,更无法等待block执行结束,因此造成死锁。

    官方文档指出:dispatch_sync的当前执行队列与提交block执行的目标队列相同时将造成死锁。
    Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

    dispatch_sync runs a block on a given queue and waits for it to complete. In this case, the queue is the main dispatch queue. The main queue runs all its operations on the main thread, in FIFO (first-in-first-out) order. That means that whenever you call dispatch_sync, your new block will be put at the end of the line, and won't run until everything else before it in the queue is done.

    The problem here is that the block you just enqueued is at the end of the line waiting to run on the main thread, but your testSample method is currently running on the main thread. The block at the end of the queue can't get access to the main thread until the current method finishes using the main thread

    ========================================

    [深入理解dispatch_sync]

    dispatch_sync使用说明
    关于dispatch_sync代码的执行逻辑,恐怕很多人都已经知道了。以如下代码为例:
    NSLog(@"step1");
    dispatch_sync(aDispatchQueue, ^{
    NSLog(@"step2");
    //block具体代码
    });
    NSLog(@"step3");

    简而言之,dispatch_sync()中的block会被同步派发,其上下文会被阻塞,直到dispatch_block派发的block被执行完成,这段代码的执行结果一定是:
    step1
    step2
    step3

    但问题是,dispatch_sync()的block的执行线程和dispatch_sync上下文所对应的线程是一个线程吗?

    所派发的block的执行线程和dispatch_sync上下文线程是同一个线程(无论上述代码中的aDispatchQueue是serial dispatch queue还是concurrent dispatch queue)。

    dispatch_sync使用注意事项

    Important: You should never call the dispatch_sync or dispatch_sync_f function from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, but should also be avoided for concurrent queues.

    相关文章

      网友评论

        本文标题:dispatch_sync

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