对行为不确定,因为我怀疑我遇到了僵局,
我有一个包含多个对象的类 - 每个对象都创建一个具有相同名称的队列。我不确定GCD是否在对象之间重用相同的队列,或者它们只是共享相同的名称。
例如
@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
答案 0 :(得分:32)
正如Apple的文档所述,标签是:
附加到队列的字符串标签,用于在诸如Instruments等调试工具中唯一标识它...
它被用作提示,仅此而已。
修改强>
以下是您使用共享队列所需的代码。
+ (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;
}
答案 1 :(得分:9)
dispatch_queue_create
正如名称所暗示的那样,它创建了一个调度队列。您提供的标签不必是唯一的,它仅用于调试目的。