ios dispatch_get_global_queue 和 dispatch_queue_create 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10984885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:43:08  来源:igfitidea点击:

What is the difference between dispatch_get_global_queue and dispatch_queue_create?

iosobjective-cmultithreadinggrand-central-dispatch

提问by Nosrettap

I'm writing a moderately complex iOS program that needs to have multiple threads for some of its longer operations (parsing, connections to the network...etc). However, I'm confused as to what the difference is between dispatch_get_global_queueand dispatch_queue_create.

我正在编写一个中等复杂的 iOS 程序,它需要有多个线程来进行一些较长的操作(解析、连接到网络等)。但是,我对dispatch_get_global_queue和之间的区别感到困惑dispatch_queue_create

Which one should I use and could you give me a simple explanation of what the difference is in general? Thanks.

我应该使用哪一个,您能否简单解释一下总体上的区别?谢谢。

回答by Rob

As the documentationdescribes, a global queue is good for concurrent tasks (i.e. you're going to dispatch various tasks asynchronously and you're perfectly happy if they run concurrently) and if you don't want to encounter the theoretical overhead of creating and destroying your own queue.

正如文档所描述的,全局队列适用于并发任务(即,您将异步分派各种任务,如果它们同时运行,您会非常高兴)并且如果您不想遇到创建和破坏你自己的队列。

The creating of your own queue is very useful if you need a serial queue (i.e. you need the dispatched blocks to be executed one at a time). This can be useful in many scenarios, such as when each task is dependent upon the preceding one or when coordinating interaction with some shared resource from multiple threads.

如果您需要一个串行队列(即您需要一次执行一个分派的块),创建您自己的队列非常有用。这在许多场景中都很有用,例如当每个任务都依赖于前一个任务时,或者在协调与来自多个线程的某些共享资源的交互时。

Less common, but you will also want to create your own queue if you need to use barriersin conjunction with a concurrent queue. In that scenario, create a concurrent queue (i.e. dispatch_queue_createwith the DISPATCH_QUEUE_CONCURRENToption) and use the barriers in conjunction with that queue. You should never use barriers on global queues.

不太常见,但如果您需要将障碍与并发队列结合使用,您还需要创建自己的队列。在这种情况下,创建一个并发队列(即dispatch_queue_create使用DISPATCH_QUEUE_CONCURRENT选项)并将屏障与该队列结合使用。你永远不应该在全局队列上使用障碍。

My general counsel is if you need a serial queue (or need to use barriers), then create a queue. If you don't, go ahead and use the global queue and bypass the overhead of creating your own.

我的一般建议是,如果您需要串行队列(或需要使用屏障),则创建一个队列。如果不这样做,请继续使用全局队列并绕过创建自己的队列的开销。



If you want a concurrent queue, but want to control how many operations can run concurrently, you can also consider using NSOperationQueuewhich has a maxConcurrentOperationCountproperty. This can be useful when doing network operations and you don't want too many concurrent requests being submitted to your server.

如果你想要一个并发队列,但又想控制可以并发运行多少个操作,你也可以考虑使用NSOperationQueuewhich有一个maxConcurrentOperationCount属性。这在进行网络操作并且您不希望向服务器提交太多并发请求时非常有用。

回答by amattn

Just posted in a different answer, but here is something I wrote quite a while back:

刚刚发布在不同的答案中,但这是我很久以前写的内容:

The best way to conceptualize queues is to first realize that at the very low-level, there are only two types of queues: serial and concurrent.

概念化队列的最好方法是首先意识到在非常低的层次上,只有两种类型的队列:串行和并发。

Serial queuesare monogamous, but uncommitted. If you give a bunch of tasks to each serial queue, it will run them one at a time, using only one thread at a time. The uncommitted aspect is that serial queues may switch to a different thread betweentasks. Serial queues always wait for a task to finish before going to the next one. Thus tasks are completed in FIFO order. You can make as many serial queues as you need with dispatch_queue_create.

串行队列是一夫一妻制的,但未提交。如果您将一堆任务分配给每个串行队列,它将一次运行一个,一次只使用一个线程。未提交的方面是串行队列可能会任务之间切换到不同的线程。串行队列总是等待一个任务完成,然后再进入下一个任务。因此,任务按 FIFO 顺序完成。您可以根据需要使用dispatch_queue_create.

The main queueis a special serial queue. Unlike other serial queues, which are uncommitted, in that they are "dating" many threads but only one at time, the main queue is "married" to the main thread and all tasks are performed on it. Jobs on the main queue need to behave nicely with the runloop so that small operations don't block the UI and other important bits. Like all serial queues, tasks are completed in FIFO order.

主队列是一个特殊的串行队列。与其他未提交的串行队列不同,它们与许多线程“约会”但一次只有一个,主队列与主线程“结合”,所有任务都在其上执行。主队列上的作业需要在运行循环中表现良好,以便小操作不会阻塞 UI 和其他重要位。像所有的串行队列,任务是在FIFO顺序完成。

If serial queues are monogamous, then concurrent queuesare promiscuous. They will submit tasks to any available thread or even make new threads depending on system load. They may perform multiple tasks simultaneously on different threads. It's important that tasks submitted to the global queue are thread-safe and minimize side-effects. Tasks are submitted for execution in FIFO order, but order of completion is not guaranteed. As of this writing, there are only three concurrent queues and you can't make them, you can only fetch them with dispatch_get_global_queue.

如果串行队列是一夫一妻制的,那么并发队列就是混杂的。他们将根据系统负载向任何可用线程提交任务,甚至创建新线程。它们可以在不同的线程上同时执行多个任务。提交到全局队列的任务是线程安全的并且最小化副作用很重要。任务按 FIFO 顺序提交执行,但不保证完成顺序。在撰写本文时,只有三个并发队列,您无法创建它们,只能使用dispatch_get_global_queue.

edit: blog post expanding on this answer: http://amattn.com/p/grand_central_dispatch_gcd_summary_syntax_best_practices.html

编辑:扩展此答案的博客文章:http: //amattn.com/p/grand_central_dispatch_gcd_summary_syntax_best_practices.html

回答by Max Yankov

One returns the existing global queue, the other creates a new one. Instead of using GCD, I would consider using NSOperation and operation queue. You can find more information about it in this guide.Typically, of you want the operations to execute concurrently, you want to create your own queue and put your operations in it.

一个返回现有的全局队列,另一个创建一个新的队列。我会考虑使用 NSOperation 和操作队列,而不是使用 GCD。您可以在本指南中找到有关它的更多信息通常,如果您希望操作并发执行,您希望创建自己的队列并将操作放入其中。