Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

ios - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...