Skip to content

Commit 36c6c69

Browse files
committed
Merge pull request #18 from jlnr/master
Initialize static variables atomically
2 parents 4745f3d + 0ad6fbd commit 36c6c69

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

JMImageCache.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
#import "JMImageCache.h"
1010

11-
static NSString *_JMImageCacheDirectory;
12-
1311
static inline NSString *JMImageCacheDirectory() {
14-
if(!_JMImageCacheDirectory) {
12+
static NSString *_JMImageCacheDirectory;
13+
static dispatch_once_t onceToken;
14+
15+
dispatch_once(&onceToken, ^{
1516
_JMImageCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/JMCache"] copy];
16-
}
17+
});
1718

1819
return _JMImageCacheDirectory;
1920
}
@@ -25,8 +26,6 @@
2526
return [JMImageCacheDirectory() stringByAppendingPathComponent:fileName];
2627
}
2728

28-
JMImageCache *_sharedCache = nil;
29-
3029
@interface JMImageCache ()
3130

3231
@property (strong, nonatomic) NSOperationQueue *diskOperationQueue;
@@ -40,9 +39,12 @@ @implementation JMImageCache
4039
@synthesize diskOperationQueue = _diskOperationQueue;
4140

4241
+ (JMImageCache *) sharedCache {
43-
if(!_sharedCache) {
42+
static JMImageCache *_sharedCache = nil;
43+
static dispatch_once_t onceToken;
44+
45+
dispatch_once(&onceToken, ^{
4446
_sharedCache = [[JMImageCache alloc] init];
45-
}
47+
});
4648

4749
return _sharedCache;
4850
}

0 commit comments

Comments
 (0)