Re: Thread safety, some basic questions about accessing mutable objects across threads
Re: Thread safety, some basic questions about accessing mutable objects across threads
- Subject: Re: Thread safety, some basic questions about accessing mutable objects across threads
- From: Kyle Sluder <email@hidden>
- Date: Tue, 06 Aug 2013 09:13:57 -0700
On Aug 6, 2013, at 8:39 AM, Nick Rogers <email@hidden> wrote:
> Hi,
>
> Please look at the following situations:
>
> 1. Created a mutable array in main thread, can I read its values in a secondary thread safely, while no other thread is modifying this mutable array?
Yes, but the second half of that sentence is the tricky part. One typically uses a lock to ensure that no other threads are modifying the array.
>
> 2. I need to add objects to this mutable array from a secondary thread, and no other thread is modifying it. Can I simply add an object like, [array addObject:obj]; or if this is wrong how to achieve the same? The object to add is mostly NSString or NSNumber or a mutable dictionary of NSString and NSNumber.
Yes, NSArray can be used from any thread, as long as if one thread is modifying the array it is the only thread accessing the array. Again, you need to use a synchronization primitive to ensure this is the case.
--Kyle Sluder
_______________________________________________
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