我创建了一个具有car
属性的单例CarQueueClass,该属性应该是线程安全的。我已经覆盖了这个属性的getter和setter。当我尝试在此类中获取self.car
或从sharedInstance
访问此属性时,它看起来像是死锁。如何在没有dispatch_sync死锁的情况下在getter中返回Car?
@interface CarQueueClass ()
@property (nonatomic, strong) Car * car;
@property (nonatomic, strong) dispatch_queue_t carQueue;
@end
@implementation CarQueueClass
@synthesize
car = _car
(id)init {
if ([super init]) {
self.carQueue = dispatch_queue_create("appName.SerialQueue", NULL);
}
return self;
}
- (void)setCar:(Car *)sampleCar
{
dispatch_async(self.carQueue, ^{
_car = sampleCar;
});
}
- (Car *)car
{
__block Car * tempCar;
dispatch_sync(self.carQueue, ^{
tempCar = _car;
});
return tempCar;
}