Skip to content

Instantly share code, notes, and snippets.

@623637646
Last active May 13, 2020 16:49
Show Gist options
  • Save 623637646/43c4561cdf128e99eda124cd79bd6c16 to your computer and use it in GitHub Desktop.
Save 623637646/43c4561cdf128e99eda124cd79bd6c16 to your computer and use it in GitHub Desktop.
iOS multiple threads is not safety with nonatomic properties.
#import <XCTest/XCTest.h>
@interface ThreadNotSafetyTests : XCTestCase
// no crash
//@property (atomic) NSObject *obj;
// crash
@property (nonatomic) NSObject *obj;
@end
@implementation ThreadNotSafetyTests
- (void)testExample {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i=0; i < 1000000; i++) {
self.obj = NSObject.new;
}
});
for (int i=0; i < 1000000; i++) {
self.obj = NSObject.new;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment