How many dispatch_queue's should one make? Less is better or more is better?

巧了我就是萌 提交于 2019-12-13 01:33:41

问题


I have 10 instances of a class which need to do some background tasks. This should happen serially for the instances, but could be concurrent in regards to that the instances can do work independent of each other.

Which is most cost effective in terms of speed and battery? When do I need to be concerned that I've created too many queues?

This one (A) ?

- (dispatch_queue_t)queue
{
    static dispatch_queue_t queue;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        queue = dispatch_queue_create("no.agens.someclass.somequeue", DISPATCH_QUEUE_SERIAL);
    });
    return queue;
}

Or this one (B) ?

// assume read-only property of 'queue' is defined

- (dispatch_queue_t)queue
{
    if(_queue == nil)
    {
        _queue = dispatch_queue_create("no.agens.someclass.somequeue", DISPATCH_QUEUE_SERIAL);
    }
    return _queue;
}

回答1:


You want (B). (A) will create a single queue, and will serialize work across ALL instances, instead of just per instance. Queues are pretty lightweight. Unless this is a class you're going to have thousands of instances of at once, I wouldn't worry about the overhead of having a queue per instance.



来源:https://stackoverflow.com/questions/23291636/how-many-dispatch-queues-should-one-make-less-is-better-or-more-is-better

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!