1、锁的封装对应关系,具体使用参考前面几篇文章
pthread_mutex_t 普通锁 | NSLock |
---|
pthread_mutex_t 递归锁 | NSRecursiveLock | pthread_mutex_t 条件锁 | NSConditionLock |
2、NSLock演示
#import "NSLockVC.h"
@interface NSLockVC ()
@property (strong, nonatomic) NSLock *lock1;
@property (strong, nonatomic) NSLock *lock2;
@end
@implementation NSLockVC
- (void)viewDidLoad {
[super viewDidLoad];
self.lock1 = [[NSLock alloc] init];
self.lock2 = [[NSLock alloc] init];
[self concurrentMoney];
[self concurrentSaleTicket];
}
- (void)__saveMoney {
[self.lock1 lock];
[super saveMoney];
[self.lock1 unlock];
}
- (void)__drawMoney {
[self.lock1 lock];
[super drawMoney];
[self.lock1 unlock];
}
- (void)__saleTicket {
[self.lock2 lock];
[super saleTicket];
[self.lock2 unlock];
}
- (void)concurrentMoney {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i = 0; i < 10; i++) {
[self __saveMoney];
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 10; i++) {
[self __drawMoney];
}
});
}
- (void)concurrentSaleTicket {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i = 0; i < 5; i++) {
[self __saleTicket];
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 5; i++) {
[self __saleTicket];
}
});
}
@end
3、NSRecursiveLock演示
#import "NSRecursiveLockVC.h"
@interface NSRecursiveLockVC ()
@property (strong, nonatomic) NSRecursiveLock *lock1;
@property (strong, nonatomic) NSRecursiveLock *lock2;
@end
@implementation NSRecursiveLockVC
- (void)viewDidLoad {
[super viewDidLoad];
[self test1];
}
- (void)test1 {
[self.lock1 lock];
static int count = 0;
NSLog(@"%s--%d",__func__,count);
if (count < 10) {
count ++;
[self test1];
}
[self.lock1 unlock];
}
@end
4、NSCondition演示
#import "NSConditioinLockVC.h"
@interface NSConditioinLockVC ()
@property (strong, nonatomic) NSCondition *condLock;
@property (strong, nonatomic) NSMutableArray *array;
@end
@implementation NSConditioinLockVC
- (void)viewDidLoad {
[super viewDidLoad];
self.array = [NSMutableArray array];
self.condLock = [[NSCondition alloc] init];
[self test];
}
- (void)test {
[[[NSThread alloc] initWithTarget:self selector:@selector(removeObj) object:nil] start];
sleep(1);
[[[NSThread alloc] initWithTarget:self selector:@selector(addObj) object:nil] start];
}
- (void)removeObj {
NSLog(@"step1---");
[self.condLock lock];
NSLog(@"step2---");
if (self.array.count == 0) {
NSLog(@"step3---");
[self.condLock wait];
}
NSLog(@"step9---");
// 开始删除元素
[self.array removeLastObject];
NSLog(@"step10--%@",self.array);
[self.condLock unlock];
NSLog(@"step11---");
}
- (void)addObj {
NSLog(@"step4---");
[self.condLock lock];
NSLog(@"step5---");
[self.array addObject:@"a"];
[self.array addObject:@"b"];
NSLog(@"step6--%@",self.array);
sleep(3);
[self.condLock signal];
NSLog(@"step7---");
[self.condLock unlock];
NSLog(@"step8---");
}
@end
5、NSConditionLock演示
#import "NSConditionVC.h"
@interface NSConditionVC ()
@property (strong, nonatomic) NSConditionLock *condLock;
@end
@implementation NSConditionVC
- (void)viewDidLoad {
[super viewDidLoad];
self.condLock = [[NSConditionLock alloc] initWithCondition:1];
[self test];
}
- (void)test {
[[[NSThread alloc] initWithTarget:self selector:@selector(addObj2) object:nil] start];
[[[NSThread alloc] initWithTarget:self selector:@selector(addObj) object:nil] start];
[[[NSThread alloc] initWithTarget:self selector:@selector(removeObj) object:nil] start];
}
- (void)removeObj {
[self.condLock lockWhenCondition:1];
NSLog(@"removeObj---");
[self.condLock unlockWithCondition:2];
}
- (void)addObj {
[self.condLock lockWhenCondition:2];
NSLog(@"addObj---");
[self.condLock unlockWithCondition:3];
}
- (void)addObj2 {
[self.condLock lockWhenCondition:3];
NSLog(@"addObj2---");
[self.condLock unlock];
}
@end
|