0

NSMutableArray is not thread-safe, but I don't see why they can't be designed to be thread-safe as long as different indices are being manipulated simultaneously. E.g. Index 1 points an instance of class X and Index 2 to another instance. Isn't it efficient to allow these two objects to be manipulated at the same time? Is this allowed when I use GCD or do I need to use a dispatch barrier when I am changing objects pointed to by different indices?

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
Smart Home
  • 801
  • 7
  • 26
  • Possible duplicate of [Is Objective-C's NSMutableArray thread-safe?](http://stackoverflow.com/questions/12098011/is-objective-cs-nsmutablearray-thread-safe) – Borys Verebskyi Mar 07 '16 at 02:58
  • What manipulations to the array do you wish to do? – rmaddy Mar 07 '16 at 03:08
  • Each element of the Array points to a "Photo" object. Initially, only the URL and name properties of the class are known. Then I start downloading the image for the URL and then apply some filters to received image data. The downloads are happening in parallel. When each DL completes, a completion block is executed to store the received data in a property of the class, then execute the filter action and store the filtered data in another property of the class. Even when I run it in a concurrent Q without dispatch barrier, I don't see a crash. Is it bcos such manipulations are ok? – Smart Home Mar 07 '16 at 05:16
  • What you're doing is fine. Concurrent reads are safe, and you aren't manipulating the array when you manipulate an object it contains. – Avi Mar 07 '16 at 06:58
  • Thanks, Avi. Does this mean only additions or removals from the array need to be protected using "barriers"? or of course, manipulating the same object pointed to by the array from two threads (i.e in my e.g, two threads changing the "photo" object pointed to by a given array index will need to be protected)? – Smart Home Mar 07 '16 at 07:01

1 Answers1

1

Think of it like this.

Your array contains pointers to objects. Pointers are essentially signposts, pointing to where the objects are located in memory.

Now when you mutate an object, you don't actually touch any pointers to the that object. The object's location in memory is unaffected.

Therefore, from the array's point of view, nothing happens when you mutate the objects themselves in it, as the pointers remain unaffected. This means that mutating different objects in an array from different threads is perfectly safe.

Therefore, you are correct when you say it's more efficient to download your data in parallel into different objects in your array.

So long as you're not mutating the array itself (adding or removing objects) or mutating the same object concurrently, you'll be fine.

If you do need to mutate the array from multiple threads simultaneously, you are right in saying you should use a concurrent queue, with a barrier to write, and a standard dispatch to read. This will allow for multiple concurrent reads (which is perfectly safe) and will serialise writes with reads.

Hamish
  • 78,605
  • 19
  • 187
  • 280