ios - multithread autorelease issue with ARC? -
a class of service has nonatomic property set in serial queue.
@interface service @property (strong, nonatomic) nsdictionary *status; @property (nonatomic) dispatch_queue_t queue; ... @end - (void)update:(nsdicationary *)paramdict { dispatch_async(self.queue, ^{ .... self.status = updatedict; } } - (void)somemethod { nsdictionary *status = self.status; } the app crashed when getter invoked, @ objc_autorelease + 6, seems runtime/clang/llvm invocation.
and crash log showed status property set on queue thread.
did crash because of lack of atomicity in accessors? if yes, how , why getter failed retaining instance? did autorelease pool drained inside synthesized nonatomic setter?
should implement getter/setter method, protecting queue/a mutex lock?
while atomic can address integrity issues of fundamental data types in multi-threaded code, it's not sufficient achieve thread-safety in general. thread-safety achieved through judicious use of locks or queues. see synchronization section of threading programming guide. or see eliminating lock-based code of concurrency programming guide describes use of queues in lieu of synchronization locks.
assuming queue serial, might able make thread-safe using following construct:
- (void)somemethod { dispatch_sync(self.queue, ^{ nsdictionary *status = self.status; // need status }); } that way, you're using serial queue synchronize access status dictionary.
by way, if queue custom concurrent, might want make sure replace dispatch_async in paramdict dispatch_barrier_async. if queue serial, dispatch_async fine.
i'd suggest try synchronizing access status, either using queue or 1 of synchronization techniques described in threading programming guide.
Comments
Post a Comment