一个应该制造多少个dispatch_queue?少即是好? [英] How many dispatch_queue's should one make? Less is better or more is better?

查看:83
本文介绍了一个应该制造多少个dispatch_queue?少即是好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的10个实例,需要执行一些后台任务.对于实例,这应该顺序发生,但是就实例可以彼此独立地工作而言,可能是并发的.

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?

这个(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;
}

还是这个(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;
}

推荐答案

您要(B). (A)将创建一个队列,并将在所有实例上而不是每个实例上序列化工作.队列非常轻巧.除非这是一个类,否则您将一次拥有成千上万个实例,那么我不必担心每个实例都有一个队列的开销.

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.

这篇关于一个应该制造多少个dispatch_queue?少即是好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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