• 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: Charles Srstka <email@hidden>
  • Date: Wed, 07 Mar 2012 21:26:35 -0600

On Mar 6, 2012, at 1:51 PM, Jan E. Schotsman wrote:

> Hello,
>
> I have an array of progress values (number objects) for subprojects, from which I calculate the overall progress .
> The array is an atomic property of the project class.
>
> Is it safe to access this array from multiple threads, using methods like objectAtIndex and replaceObjectAtIndex?

(Resending since my original reply, written yesterday, still hasn’t posted; apologies if this ends up showing up twice on the list.)

It’s not; however, if you implement the KVO indexed accessors for to-many properties, described here:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/AccessorConventions.html#//apple_ref/doc/uid/20002174-178830-BAJEDEFB

and guard each accessor with a mutex, spinlock, dispatch_sync, or something similar, you should then be able to use -mutableArrayValueForKey: to return an NSMutableArray proxy that you can use like an ordinary NSMutableArray, but which will call your now-atomic indexed accessors to get at the contents of the array. Just make sure all accessors are properly thread-safe and that none of them return the actual ivar backing the property, and you should be fine.

Charles
_______________________________________________

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


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

  • Prev by Date: Re: Finding object array index when iterating through array
  • Next by Date: Why are 2 Terminal windows opening with NSAppleScript?
  • Previous by thread: Re: Accessing array in thread safe way
  • Next by thread: Re: Accessing array in thread safe way
  • Index(es):
    • Date
    • Thread