dispatch_sync调用进入dispatch_async调用

我对这个代码的行为有些怀疑:

dispatch_async(queue, ^{ sleep(2); NSLog(@"step1"); dispatch_sync(queue, ^{ sleep(3); NSLog(@"step 2"); }); NSLog(@"step 3"); }); 

从这些行我希望得到作为输出step1 -> step3 -> step2但我只获得step1

如果我改变dispatch_sync与dispatch_async它按预期工作,dispatch_sync调度dispatch_async调用创build这种问题?

回答后编辑—————-

这种情况造成了一个僵局:

您可以查看接受的答案来解释这种情况,并检查此链接的文档http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dispatch_async.3.html

这是一个僵局。

dispatch_sync调用将等待,直到queue可用,然后再运行其块并返回,但在dispatch_async完成之前不会可用,因此它只会坐在那里等待调用dispatch_sync

正如@mattjgalloway所说,这是一个僵局。

苹果自己的文档在这里提到了这个问题: http : //developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dispatch_async.3.html (见“RECURSIVE LOCKS”)。 这是在recursion锁的背景下讨论的,但原理是一样的。