How much dispatch_queue should I do? Is less better or better?

I have 10 instances of a class that should perform some background tasks. This should occur sequentially for instances, but may be concurrent with respect to the fact that instances can perform work independently of each other.

What is the most cost-effective in terms of speed and battery? When do I need to worry about creating 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
source share
1 answer

You want (B). (A) create a single queue and serialize the work in all instances, not just one instance. The lines are pretty easy. If this is not a class, you will have several instances at once, I would not worry about the overhead of having a queue for one instance.

+3
source

All Articles