Skip to content

Instantly share code, notes, and snippets.

@hadhi631
Created September 12, 2018 20:39
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 hadhi631/95e4c971233b49777c59226661224176 to your computer and use it in GitHub Desktop.
Save hadhi631/95e4c971233b49777c59226661224176 to your computer and use it in GitHub Desktop.
sync2mutex
@implementation TestMutex {
NSObject *mutex_1;
NSObject *mutex_2;
}
- (instancetype)init {
if (self = [super init]) {
mutex_1 = [NSObject new];
mutex_2 = [NSObject new];
}
return self;
}
// Thread A and Thread B will be calling this and will not block
// Thread C and Thread D from calling secondMethod
- (void)firstMethod {
@synchronized(mutex_1) {
// Deals with Resource 1
}
}
// Thread C and Thread D will be calling this and will not block
// Thread A and Thread B from calling firstMethod
- (void)secondMethod {
@synchronized(mutex_2) {
// Deals with Resource 2
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment