Block in @synchronized
Executing a block inside @synchronized
seems to negate the lock.
- (void)method {
@synchronized(self) {
if(ivar == nil) {
ivar = [myBlock() retain];
}
}
}
The instance variable ivar
is not written in any other location.
I've observed that the block myBlock
sometimes is executed twice in my application.
How can this ever happen? How to avoid this an do a real workin开发者_StackOverflow社区g lock?
Maybe you could move the locking inside the block.
The lock worked fine as synchronized
locks only threads, and the same thread was accessing the region twice. The problem was, that myBlock
executed itself inside under some circumstances.
精彩评论