在GCD(大型中央调度)中创建create dispatch_queues的数量是多少? [英] How many is too many for create dispatch_queues in GCD (grand central dispatch)?

查看:96
本文介绍了在GCD(大型中央调度)中创建create dispatch_queues的数量是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mike Ash撰写了一篇有关Swift内置的轻量级通知系统的精彩文章:(

There is a wonderful article about a lightweight notification system built in Swift, by Mike Ash: (https://www.mikeash.com/pyblog/friday-qa-2015-01-23-lets-build-swift-notifications.html).

基本思想是创建可以监听"对象,即在状态发生某些变化时调用回调.为了使其具有线程安全性,创建的每个对象都拥有自己的dispatch_queue. dispatch_queue仅用于门控关键部分:

The basic idea is you create objects that you can "listen" to i.e. invoke a callback on when there is some state change. To make it thread-safe, each object created holds its own dispatch_queue. The dispatch_queue is simply used to gate critical sections:

dispatch_sync(self.myQueue) {
    // modify critical state in self
}

此外,它可能不会引起激烈的争论.我为您创建的每个单个对象(可以被监听)创建自己的调度队列而感到震惊,只是出于锁定几行代码的目的.

and moreover it likely won't be in high contention. I was kind of struck by the fact that every single object you create that can be listened to makes its own dispatch queue, just for the purposes of locking a few lines of code.

一位张贴者建议OS_SPINLOCK更快更便宜.也许,但是肯定会占用更少的空间.

One poster suggested an OS_SPINLOCK would be faster and cheaper; maybe, but it would certainly use a lot less space.

如果我的程序创建了成百上千个(甚至成千上万个对象),我是否应该担心创建这么多调度队列?也许大多数人甚至都不会听,但是有些人可能会听.

If my program creates hundreds or thousands (or even tens of thousands of objects) should I worry about creating so many dispatch queues? Probably most won't ever even be listened to, but some might.

两个对象不会互相阻塞(即具有单独的锁)当然是有道理的,通常我不会在每个对象中都嵌入一个pthread_mutex,而是整个调度队列,三思而后行?真的可以吗?

It certainly makes sense that two objects not block each other, i.e. have separate locks, and normally I wouldn't think twice about embedding, say, a pthread_mutex in each object, but an entire dispatch queue? is that really ok?

推荐答案

Well, the documentation on Grand Central Dispatch is fairly vague about the inner workings & the exact costs of dispatch queues, however it does state that:

GCD提供并管理FIFO队列,您的应用程序可以以块对象的形式向其提交任务. 提交给调度队列的块是在系统完全管理的线程池上执行的.

因此,听起来好像队列不过是用于通过线程池对块进行排队的接口,因此在空闲时对性能没有/有最小的影响.

So, it sounds like queues are no more than an interface for queueing blocks through a thread pool, and therefore have no/minimal impact on performance when idle.

概念性文档还指出:

您可以根据需要创建任意数量的串行队列

You can create as many serial queues as you need

这听起来确实像在创建串行调度队列并将其闲置几乎是微不足道的.

Which definitely sounds like there's almost a trivial cost with creating serial a dispatch queue, and leaving it idle.

此外,我决定测试是否在具有某些Open GL内容的应用程序上创建10,000个串行和并发调度队列,但没有发现性能受到任何影响,FPS保持不变,并且仅使用了额外的4MB RAM(单个队列约为400个字节).

Furthermore, I decided to test creating 10,000 serial and concurrent dispatch queues on an app with some Open GL content, and didn't find that the performance was impacted in any way, the FPS remained the same, and it only utilised an extra 4MB of RAM (~400 bytes for a single queue).

就使用OS_SPINLOCK而不是调度队列而言,Apple在

In terms of using an OS_SPINLOCK instead of dispatch queues, Apple is very clear in it's documentation about migrating away threads that GCD is more efficient than using standard locks (at least in contended cases).

用队列替换基于锁的代码消除了许多与锁相关的惩罚,还简化了剩余的代码.您可以使用创建队列来序列化访问该资源的任务,而不必使用锁来保护共享资源. 队列不施加与锁相同的惩罚.例如,排队任务不需要陷入内核即可获取互斥体.

Replacing your lock-based code with queues eliminates many of the penalties associated with locks and also simplifies your remaining code. Instead of using a lock to protect a shared resource, you can instead create a queue to serialize the tasks that access that resource. Queues do not impose the same penalties as locks. For example, queueing a task does not require trapping into the kernel to acquire a mutex.

尽管还值得注意的是,如果您不使用队列,可以随时释放队列;如果担心内存,可以在以后需要再次使用时重新创建队列.

Although it's also worth noting that you can always release a queue if you're not using it and re-create it later when it needs using again, if you are concerned about memory.

发送队列是解决方法.您不必太担心创建大量队列而不使用它们,它们肯定比锁更有效.

Dispatch queues are the way to go. You don't need to worry too much about creating lots of queues and not using them, and they're certainly more efficient than locks.

实际上,您发现在无竞争情况下自旋锁的速度更快,因此您可能希望将其用于此目的!

You actually found that a spinlock is faster in un-contended situations, so you'll probably want to use that for this!

这篇关于在GCD(大型中央调度)中创建create dispatch_queues的数量是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆