创建具有相同名称的串行队列的2个对象共享同一队列 [英] Do 2 objects creating serial queues with the same name share the same queue

查看:146
本文介绍了创建具有相同名称的串行队列的2个对象共享同一队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对行为不确定,因为我怀疑我遇到了死锁,

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

我有一个包含多个对象的类 - 每个对象创建一个具有相同名称的队列。我不确定GCD是否在对象之间重用相同的队列,或者它们只是共享相同的名称。

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.

例如

@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


推荐答案

正如Apple的文档所述,标签是:

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.

编辑

以下是使用共享队列所需的代码。

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

这篇关于创建具有相同名称的串行队列的2个对象共享同一队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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