ios - Can we allow different "indexes" of a NSMutableArray to be manipulated simultaneously from two threads -
nsmutablearray
not thread-safe, don't see why can't designed thread-safe long different indices being manipulated simultaneously. e.g. index 1 points instance of class x , index 2 instance. isn't efficient allow these 2 objects manipulated @ same time? allowed when use gcd or need use dispatch barrier when changing objects pointed different indices?
think of this.
your array contains pointers objects. pointers signposts, pointing objects located in memory.
now when mutate object, don't touch pointers object. object's location in memory unaffected.
therefore, array's point of view, nothing happens when mutate objects in it, pointers remain unaffected. this means mutating different objects in array different threads safe.
therefore, correct when it's more efficient download data in parallel different objects in array.
so long you're not mutating array itself (adding or removing objects) or mutating same object concurrently, you'll fine.
if do need mutate array multiple threads simultaneously, right in saying should use concurrent queue, barrier write, , standard dispatch read. allow multiple concurrent reads (which safe) , serialise writes reads.
Comments
Post a Comment