iOS线程安全getter死锁

时间:2016-03-22 17:04:50

标签: ios objective-c multithreading

我创建了一个具有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;
}

0 个答案:

没有答案