Re: Accessing array in thread safe way
Re: Accessing array in thread safe way
- Subject: Re: Accessing array in thread safe way
- From: Per Bull Holmen <email@hidden>
- Date: Wed, 07 Mar 2012 07:29:19 +0100
Den 23:10 6. mars 2012 skrev Marco Tabini <email@hidden> følgende:
> On 2012-03-06, at 2: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?
>>
>
> NSMutableArray is not (as far as I know) thread-safe while being mutated[1]. That said, it doesn't mean that you can't use the array in a multi-threaded environment: Just make sure that writes are all synchronized, and that enumerations only happen on immutable copies.
>
> HTH,
Perhaps OP thought that just declaring the array as an atomic property
would make it thread-safe. I believe this would happen if
getters/setters were synthesized, and the array contents was only
changed by creating a whole new array, and always replacing it using
the setter. You would typically make the array immutable for such an
approach. In general, this can be a decent way of doing things in a
multithreaded app, because time spent in synchronized code would be
minimized. But I don't know if there are Cocoa-specific reasons to
avoid it, and of course it depends on how feasible it is for the
application.
Per
_______________________________________________
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