简体   繁体   English

iOS GCD:主线程从主队列以外的队列中出列块?

[英]iOS GCD: main thread dequeue blocks from queues other than main queue?

I know queue is NOT thread. 我知道队列不是线程。 Queue is a higher level concept than thread in GCD. 队列是一个比GCD中的线程更高级别的概念。 Programmers only deal with queues, and let system decides which threads to execute blocks for maximum efficiency. 程序员只处理队列,让系统决定执行哪些线程以获得最大效率。

And I know main queue binds to main thread, which means if I put a block on main queue, only main thread will dequeue and run it. 我知道主队列绑定到主线程,这意味着如果我在主队列上放置一个块,只有主线程将出列并运行它。 My question is: is it possible that main thread dequeue blocks from queues (serial or concurrent) OTHER THAN main queue? 我的问题是:除了主队列之外,主线程是否可能从队列(串行或并发)中出列块?

Question: "Is it possible that main thread dequeue blocks from queues (serial or concurrent) OTHER THAN main queue?" 问题:“除了主队列之外,主线程是否可能从队列(串行或并发)中出列块?”

Answer: "No." 答:“不。”

If GCD allowed blocks submitted to the global concurrent queues to run on the main thread then those blocks might also execute long-running operations which blocked the UI, and that would be both bad and counter to GCD's own design principles. 如果GCD允许提交给全局并发队列的块在主线程上运行,那么这些块也可能执行阻塞UI的长时间运行操作,这既不好又违背GCD自己的设计原则。 It's also not how it is documented to work (and the source code is always a good reference if you want to know exactly how GCD works, since GCD is also open source). 这也是不它是如何记录工作(和源代码始终是一个很好的参考,如果你想知道究竟 GCD是如何工作的,因为GCD也是开源的)。 The only time that a block is likely to be executed on the current thread as an optimization is in the dispatch_sync() case since it's clear that the programmer does not intend to return to the current thread until that block, and any enqueued blocks before it, are done in any case and blocking the current thread is the expected behavior, so there is no surprise there. 作为优化的一个块可能在当前线程上执行的唯一一次是在dispatch_sync()情况下,因为很明显程序员不打算在该块之前返回当前线程,并且在它之前的任何排队块都是,在任何情况下都完成并且阻塞当前线程是预期的行为,所以毫无意外。 The same is clearly not true for dispatch_async() since asynchronous behavior is clearly desired with that API. 对于dispatch_async()来说,情况显然不同,因为该API显然需要异步行为。

Finally, just to clear up one other point of confusion in that answer, dispatch_queue_create() does not only create serial queues - it can also be used to create concurrent queues, so concurrent queue execution is not the sole providence of global concurrent queues! 最后,为了清除该答案中的另一个混淆点,dispatch_queue_create()不仅创建了串行队列 - 它还可以用于创建并发队列,因此并发队列执行不是全局并发队列的唯一功能! See the 2nd argument - it can be set to DISPATCH_QUEUE_CONCURRENT to create a concurrent queue (the man page is a bit outdated, but the HeaderDoc in /usr/include/dispatch/queue.h is authoritative). 请参阅第二个参数 - 可以将其设置为DISPATCH_QUEUE_CONCURRENT以创建并发队列(手册页有点过时,但/usr/include/dispatch/queue.h的HeaderDoc是权威的)。

It is possible if you use dispatch_sync . 如果使用dispatch_sync ,则可以。 The documentation for dispatch_sync says this: dispatch_sync的文档说明了这一点:

As an optimization, this function invokes the block on the current thread when possible. 作为优化,此函数在可能的情况下调用当前线程上的块。

So if you use dispatch_sync on the main thread, it may execute the block on the main thread. 因此,如果在主线程上使用dispatch_sync ,它可以在主线程上执行块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM