Wednesday, October 7, 2026
HomeiOS Developmentios - Is it secure to move a lazily initialized object to...

ios – Is it secure to move a lazily initialized object to @synchronized as lock token in Goal-C?

[ad_1]

Here is my code snippet:

@interface MyClass ()

@property (nonatomic, sturdy) NSMutableDictionary *dict;

@finish

@implementation MyClass

- (NSMutableDictionary *)dict {
    if (_dict == nil) {
        _dict = [NSMutableDictionary dictionary];
    }
    return _dict;
}

- (void)funcationA {
    @synchronized (self.dict) {
        // do one thing
    }
}

- (void)functionB {
    @synchronized (self.dict) {
        // do one thing
    }
}

@finish

If functionA and functionB can execute in numerous threads, I ponder if this @synchronize() actually works. For my part, it might occur when two threads are executing @synchronized (self.dict) on the identical time earlier than dict is initialized, so two situations are created, and the 2 @synchronize() are utilizing two totally different locks beneath the hood. That is solely my guess and I do not know the way to write a demo to reveal this.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments