Skip to content

Instantly share code, notes, and snippets.

@soxjke
Last active March 27, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soxjke/ff5f8d39d5ea6928bd2b2a04f7b5832b to your computer and use it in GitHub Desktop.
Save soxjke/ff5f8d39d5ea6928bd2b2a04f7b5832b to your computer and use it in GitHub Desktop.
Race condition
@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