Yes, you do need to use the same NSLock instance to lock both accesses to the array. As with so many multithreading errors, the problem may have seemed to disappear due to timing differences caused by adding the additional code. Or, it could just be that you got (un)lucky and the problem didn't appear when you tested the second time.
For whatever it's worth, NSLock is just one way of locking access to critical sections in Objective-C. You can also use @synchronized()
which may be easier from a code-complexity point of view:
@synchronized(someSharedToken) {
// Do your array access here
}
You could also use a serial dispatch queue to serialize access to the resource. That has several advantages, not least of which is the ability to dispatch work to it without waiting for that work to finish on the current thread. It's also less expensive to dispatch work to a queue than it is to take out a lock. See the Creating Serial Dispatch Queues section of Apple's Concurrency Programming Guide.