简体   繁体   中英

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. 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. 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). 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. The same is clearly not true for dispatch_async() since asynchronous behavior is clearly desired with that 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! 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).

It is possible if you use dispatch_sync . The documentation for dispatch_sync says this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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