chris P
chris P

Reputation: 6589

What's the best practice and accepted way to solve this threading and data integrity issue?

Ok, let's imagine the following class

Code Here

Now, doing this we would have a problem. Our problems are on lines 58&65. Let's take line 58.

We are wrapping mutating of our NSMutableArray in a dispatch_async call to a serial queue. However, when we call [self myMutableArray], that will dispatch_sync to the same serial queue. That dispatch code will never be called, because our first dispatch code will not complete first.

What is the ideal solution to this

1) remove - (NSMutableArray *)myMutableArray and - (void)setMyMutableArray:(NSMutableArray *)myMutableArray. Keep all reads in dispatch_sync to the serial queue, and all writes in dispatch_async calls to the serial queue... with the exception of the setter and getter. Is it common to never wrap the setter and getter?

That's actually the only solution I believe.

So my question is... is it common practice to never wrap the getter and setter internals in dispatch's to your serial queue?

Upvotes: 0

Views: 62

Answers (1)

Fonix
Fonix

Reputation: 11597

your code has created a deadlock because calling sync inside an async serial queue that is on the same queue will cause a deadlock, because the outer block is waiting for the inner block to finish, but the inner block cant execute because its on the back of the queue.

its probably best to not have the getter wrapped in a queue, maybe you can leave the setter though

Upvotes: 0

Related Questions