在苹果文档中,它说:
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调用到同一个队列.