开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜