Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do 2 objects creating serial queues with the same name share the same queue

Not sure on the behavior, because I suspect I am getting a deadlock,

I have a class with multiple objects - each object creates a queue with the same name. I'm not sure if GCD is reusing the same queue between the objects or if they just share the same name.

For instance

@interface MyClass  -(void)doSomeWork @property (nonatomic,strong) dispatch_queue_t myQueue;  @end  @implementation MyClass  -(id)init {   self = [super init];   self.myQueue = dispatch_queue_create("MyQueue",DISPATCH_QUEUE_SERIAL);   return self; }  -(void)doSomeWork {   dispatch_async(self.myQueue,^{     // some long running work   }); }  @end   @interface SomeClassWhichCreatesALotOfObjects  @end   @implementation SomeClassWhichCreatesALotOfObjects  -(void)someMethod {   for(int i = 0; i < 10000; i++)   {     MyClass *object = [MyClass new];     [object doSomeWork]; // are these running in serial to each other or are each offset to the queue their object has created? Can't understand from the debugger   } } @end 
like image 844
Avner Barr Avatar asked Oct 17 '13 08:10

Avner Barr


People also ask

How do you create a serial queue?

serial when creating a serial queue: let serialQueue = DispatchQueue(label: "queuename") . In Xcode 8 beta 4 there is no . serial option so you have to create serial queue by omitting the . concurrent in attributes.

What is difference between serial and concurrent queue?

Since its concurrent queue, tasks may not finish in the order they are added to queue. But with synchronous operation it does although they may be processed by different threads. So, it behaves as this is the serial queue. Remember using GCD you are only adding task to the Queue and performing task from that queue.

Is global queue serial or concurrent?

Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue.


1 Answers

As Apple's documentation states, the label is:

A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments...

It is used as a hint, nothing more.

EDIT

Here's the code you want for using a shared queue.

+ (dispatch_queue_t)sharedQueue {     static dispatch_queue_t sharedQueue;     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         sharedQueue = dispatch_queue_create("MyQueue", NULL);     });     return sharedQueue; } 
like image 117
Guy Kogus Avatar answered Sep 17 '22 00:09

Guy Kogus