Instantly share code, notes, and snippets.

Embed
What would you like to do?
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