Re: Accessing array in thread safe way
Re: Accessing array in thread safe way
- Subject: Re: Accessing array in thread safe way
- From: Jens Alfke <email@hidden>
- Date: Tue, 06 Mar 2012 17:03:36 -0800
On Mar 6, 2012, at 11:51 AM, Jan E. Schotsman wrote:
> Is it safe to access this array from multiple threads, using methods like objectAtIndex and replaceObjectAtIndex?
It depends. The short answer: you should use your own synchronization around your own critical sections instead of relying on the thread-safety of lower-level objects.
A longer answer: The array object itself is safe to the extent that you won’t corrupt its state by accessing it from multiple threads. On the other hand, you’re likely to have trouble at a higher level unless you add your own synchronization around your code that accesses the array. For example, one thread might call -objectAtIndex: on an index that used to be safe, but another thread got in the way and removed an object, making the index out of range and throwing an exception. Or worse, you might get an object from an array, only to have it removed from the array by another thread before you can operate on it.
Personally, I avoid sharing mutable data between threads if I possibly can. It can quickly turn into an unholy mess.
—Jens
_______________________________________________
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