[TOC]
一. 最简单的NSLock
@interface LiLockController ()
@property(nonatomic,strong)NSLock * lock;
@end
@implementation LiLockController
- (void)viewDidLoad {
[super viewDidLoad];
self.lock = [[NSLock alloc]init];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lock];
for (int i = 0; i < 10; i ++) {
NSLog(@"------1111111111------");
}
[self.lock unlock];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lock];
for (int i = 0; i < 10; i ++) {
NSLog(@"++++++22222222+++++++");
}
[self.lock unlock];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lock];
for (int i = 0; i < 10; i ++) {
NSLog(@"*******33333333*******");
}
[self.lock unlock];
});
}
@end
二.NSCondition : NSObject <NSLocking>
条件锁 满足某个条件下才能执行某个操作
@interface LiLockController ()
@property(nonatomic,strong)NSCondition * conditionLock;
@property(nonatomic,strong)NSMutableArray * dataSource;
@end
@implementation LiLockController
- (void)viewDidLoad {
[super viewDidLoad];
self.conditionLock = [[NSCondition alloc]init];
self.dataSource = [NSMutableArray array];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.conditionLock lock];
if (self.dataSource.count == 0) {
[self.conditionLock wait];
}
[self.dataSource addObject:@"999"];
NSLog(@"第二个网络请求");
[self.conditionLock signal];
[self.conditionLock unlock];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (self.dataSource.count < 2) {
[self.conditionLock wait];
}
NSLog(@"第三个网络请求");
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.conditionLock lock];
[self.dataSource addObject:@"888"];
NSLog(@"第一个网络请求");
[self.conditionLock signal];
[self.conditionLock unlock];
});
}
三.串行队列
dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i = 0; i < 10; i ++) {
NSLog(@"------1111111111------");
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 10; i ++) {
NSLog(@"++++++22222222+++++++");
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 10; i ++) {
NSLog(@"*******33333333*******");
}
});
四.信号量
@interface LiLockController ()
@property (nonatomic,strong)dispatch_semaphore_t semaphore;
@end
@implementation LiLockController
- (void)viewDidLoad {
[super viewDidLoad];
self.semaphore = dispatch_semaphore_create(1);//只允许一条线程访问
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"------1111111111------");
}
dispatch_semaphore_signal(self.semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"++++++22222222+++++++");
}
dispatch_semaphore_signal(self.semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"*******33333333*******");
}
dispatch_semaphore_signal(self.semaphore);
});
}
五.@synchronized
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"------1111111111------");
}
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"++++++22222222+++++++");
}
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"*******33333333*******");
}
}
});
}
*****************************************************************
递归锁🔐
- (void)otherTest
{
@synchronized(self) {
NSLog(@"123");
[self otherTest];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self otherTest];
}
六.GCD栅栏函数实现多读单写
@interface LiLockController ()
@end
@implementation LiLockController
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_queue_create("rw_Queue", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 10; i++) {
dispatch_async(queue, ^{
[self read];
});
dispatch_async(queue, ^{
[self read];
});
dispatch_async(queue, ^{
[self read];
});
dispatch_barrier_async(queue, ^{
[self write];
});
}
}
- (void)read
{
NSLog(@"---读---");
}
- (void)write
{
NSLog(@"***写***");
}
@end
网友评论