Re: Accessing array in thread safe way
Re: Accessing array in thread safe way
- Subject: Re: Accessing array in thread safe way
- From: Fritz Anderson <email@hidden>
- Date: Tue, 06 Mar 2012 17:43:19 -0600
On 6 Mar 2012, at 1:51 PM, Jan E. Schotsman wrote:
> Is it safe to access this array from multiple threads, using methods like objectAtIndex and replaceObjectAtIndex?
You can access immutable Foundation objects across threads, but not mutable ones, which is what you're thinking of. Your threads _will_ attempt to alter the array simultaneously, or to access elements while others are doing insertions and deletions. Referring to the array through an atomic accessor doesn't help: Your problem isn't with assigning into the instance variable, it's with changes to the array itself.
There are strategies to deal with this. I can think of many, but I don't have time to lay them out, or even to determine which of them are half-baked. It's a solved problem, but the solutions are not pretty compared to the hope that immediate parallel access will just work.
> I have an array of progress values (number objects) for subprojects, from which I calculate the overall progress.
Try keeping an array of whatever objects represent the subtasks, each of which would have an atomic accessor to its count. Then have your overall object poll them.
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden