微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 如何在Grand Central Dispatch中创建一个僵局?

在苹果文档中,它说:

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_queue_t queue = dispatch_queue_create("my.label",disPATCH_QUEUE_SERIAL);
dispatch_async(queue,^{
    dispatch_sync(queue,^{
        // outer block is waiting for this inner block to complete,// inner block won't start before outer block finishes
        // => deadlock
    });

    // this will never be reached
});

这里很清楚,外部和内部块在同一个队列上运行.大多数情况下会发生这种情况的地方,dispatch_sync的调用者的队列不太明显.这通常发生在(深入)嵌套的堆栈中,您正在某些类中执行代码,这些代码最初是在某个队列中启动的,而且您偶然会将dispatch_sync调用到同一个队列.

原文地址:https://www.jb51.cc/iOS/329676.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐