I am really fond of @synchronized-construct instead of using NSLocks, when I want that a data structure is only read or written by one thread at a time. However, when I want to change the whole object we are @synchronizing on, I am not sure if @synchronized is suitable. What I want to know, is whether these work the same way:
// version 1:
// segment 1:
@synchronized(self.obj) {
self.obj = obj2;
}
// segment 2:
@synchronized(self.obj) {
// some other stuff
}
// version 2:
// segment 1:
[objLock lock];
self.obj = obj2;
[objLock unlock];
// segment 2:
[objLock lock];
// some other stuff
[objLock unlock];