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

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

概述在苹果文档中,它说: 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 particu 在苹果文档中,它说:

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调用到同一个队列.

总结

以上是内存溢出为你收集整理的ios – 如何在Grand Central Dispatch中创建一个僵局?全部内容,希望文章能够帮你解决ios – 如何在Grand Central Dispatch中创建一个僵局?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1096347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存