What Jeremip said is true. There are other things to consider. If the first code fragment will work for you, then we can conclude that you are not interested in any intermediate states during (long block of code). (i.e. you are not reading from this array outside the @synchronized block, are you?) In this case, it would be better for you to perform operations that need to be synchronized and do them all at once. For instance:
.. id objectToAdd = [NSNumber numberWithInt: 203]; .. @synchronized (someObj) { [someObj removeAllObjects] [someObj addObject:objectToAdd]; }
Put in general: Concurrency, and performance will improve by reducing how many times you take a lock, and how long you hold it, holding it when you take it. In this trivial example, there is nothing that indicates that we could not take this lock once, not twice, and, in addition, there was no indication that we could not perform operations such as stated above, explicitly storing all code that does not require synchronization, outside the synchronized block.
It says, "Make it work, do it right, make it work fast." - in this sequence. I would not spend much time / energy optimizing your lockout strategy until you measure your application and see that a lock conflict is a problem.
ipmcc source share