Last active
March 27, 2017 13:11
-
-
Save soxjke/ff5f8d39d5ea6928bd2b2a04f7b5832b to your computer and use it in GitHub Desktop.
Race condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface StringContainer : NSObject | |
- (instancetype)initWithString:(NSString *)string; | |
@property (nonatomic, unsafe_unretained) NSString *string; | |
@end | |
@implementation StringContainer | |
- (instancetype)initWithString:(NSString *)string { | |
self = [super init]; | |
if (self) { | |
self.string = string; | |
} | |
return self; | |
} | |
@interface Some : NSObject { | |
NSString *stringProp; | |
} | |
@end | |
@end | |
@implementation Test | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
dispatch_async(dispatch_get_global_queue(0, 0), ^{ | |
[self test]; | |
}); | |
} | |
return self; | |
} | |
- (void)test { | |
for(int i = 0; i < 1000; i++) { | |
dispatch_async(dispatch_get_global_queue(0, 0), ^() { | |
stringProp = [[StringContainer alloc] initWithString:[[NSString alloc] initWithFormat:@"%d", i]]; | |
}); | |
dispatch_async(dispatch_get_global_queue(0, 0), ^() { | |
[self print:stringProp]; | |
}); | |
} | |
} | |
- (void)print:(StringContainer * __unsafe_unretained)string { | |
NSLog(@"%@", string.string); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment