• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessing array in thread safe way
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Accessing array in thread safe way (From: "Jan E. Schotsman" <email@hidden>)

  • Prev by Date: Re: JSON validator for Xcode
  • Next by Date: Re: Accessing array in thread safe way
  • Previous by thread: Re: Accessing array in thread safe way
  • Next by thread: Re: Accessing array in thread safe way
  • Index(es):
    • Date
    • Thread