In the below code we have a mutable array, which is being mutated by two concurrent queues. Since concurrent queues are not thread safe, this code should ideally crash but this gets executed without any exception or crash.
Kindly help me in understanding this behaviour. Any help will be much appreciated :-)
@interface ViewController ()
@property(nonatomic, strong) NSMutableArray *arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arr = [NSMutableArray new];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i < 20000; i++) {
[weakSelf.arr addObject:[NSNumber numberWithInt:i]];
NSLog(@"Added %@", [weakSelf.arr lastObject]);
}
NSLog(@"Final count %ld", [self.arr count]);
});
[self performSelector:@selector(removeObjects) withObject:nil afterDelay:0.1];
}
-(void)removeObjects{
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i = 0; i < 1000; i++) {
if (weakSelf.arr.count > 1) {
[weakSelf.arr removeObjectAtIndex:0];
}
NSLog(@"Remove object");
}
});
}
@end
Having concurrent access to one resource does not mean, that a crash is guaranteed. This has nothing to do with "having luck". Maybe your code runs systematically in a situation, where no crash occurs.
Moreover being "not thread-safe" does not mean "will crash". Any malfunction can occur.