Created
October 5, 2018 00:56
-
-
Save juwencheng/ddeed34c4cec119440aa38f296fe78d5 to your computer and use it in GitHub Desktop.
iOS多线程问题演示
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
MultiThreadObject *object = [[MultiThreadObject alloc] init]; | |
NSLock *lock = [[NSLock alloc] init]; | |
[NSThread detachNewThreadWithBlock:^{ | |
NSInteger loopCount = 1000; | |
while (loopCount) { | |
[lock lock]; | |
NSArray *data = @[@"1", @"2", @"3"]; | |
object.data = data; | |
if (object.data != data) { | |
NSLog(@"多线程同步问题 self.data: %@, data: %@",object.data, data); | |
} | |
loopCount --; | |
[lock unlock]; | |
} | |
}]; | |
[NSThread detachNewThreadWithBlock:^{ | |
NSInteger loopCount = 1000; | |
while (loopCount) { | |
[lock lock]; | |
NSArray *data = @[@"1", @"2", @"3", @"4"]; | |
object.data = data; | |
loopCount --; | |
[lock unlock]; | |
} | |
}]; |
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 MultiThreadObject: NSObject | |
@property (nonatomic, strong) NSArray *data; | |
@end | |
@implementation MultiThreadObject | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
重现步骤
object
,其类型为MultiThreadObject
object.data
设值,在同一个线程中,设置值后立马取出来和传入参数进行对比