多读单写
理解多读单写:
- 同时有多个读操作,读操作的时候,不能有写的操作;
- 在写操作时,不能有别的写操作;在写操作之前,所有的读操作都完成;
- 读操作是并行的,写操作时互斥的;
一、采用dispatch_barrier实现;
@interface Test () {
dispatch_queue_t current_queue;
NSMutableDictionary *dic;
}
@end
@implementation Test
- (id)init {
self = [super init];
if (self) {
dic = [[NSMutableDictionary alloc] init];
// 创建并行队列
current_queue = dispatch_queue_create("testQueue", DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
- (void)setSafeObject:(id)object forKey:(NSString *)key {
key = [key copy];
// 异步栅栏写数据
// barrier阻塞队列
dispatch_barrier_async(current_queue, ^{
if (key && object) {
[dic setObject:object forKey:key];
}
});
}
- (id)getSafeObjectForKey:(NSString *)key {
__block id result = nil;
// 同步读取指定数据 (同步并行)
dispatch_sync(current_queue, ^{
result = [dic valueForKey:key];
});
return result;
}
- (BOOL)containObject:(id)object {
__block BOOL isExist;
//同步读取
dispatch_sync(current_queue, ^ {
isExist = [dic.allValues containsObject: object]
});
}
@end
- 总结
1- 读操作:为什么用dispatch_sync?
一般读操作通常是直接想要结果,需要`同步返回`;如果`异步`获取的话,可能`会延迟`;
2- 写操作:为什么写用dispatch_barrier_async?
异步栅栏dispatch_barrier_async会阻塞`读写操作的队列`,
不会阻塞主队列和其他队列,不会影响其他操作;
二、采用rwlock读写锁实现
pthread_rwlock_t lock; //声明
pthread_rwlock_init(&lock,NULL); //初始化
pthread_rwlock_rdlock(&lock);//加读锁,可以加锁个
pthread_rwlock_wrlock(&lock); //加写锁,互斥的,只能加一个
pthread_rwlock_unlock(&lock); //解锁
示例代码:
#import <pthread.h>
@interface Test() {
pthread_rwlock_t lock;//读写锁
NSMutableDictionary *dic;
}
@end
@impletation
- (id)init {
self = [super init];
if (self) {
[self initTestRwLock];
}
return self;
}
- (void)initTestRwLock {
pthread_rwlock_init(&lock,NULL); //1- 初始化读写锁
dic = [NSMutableDictionary dictionary];
}
- (id)objectForKey:(NSString *)key {
pthread_rwlock_rdlock(&lock); //加读锁
NSLog(@"读写锁:读操作-加锁: %@",key);
id obj = [dic objectForKey:key];
sleep(2);
pthread_rwlock_unlock(&lock); //解锁
NSLog(@"读写锁:读操作-解锁: %@",key);
return obj;
}
- (void)setObject:(id)obj forKey:(NSString *)key {
pthread_rwlock_wrlock(&lock); //加写锁,写锁是互斥的
NSLog(@"读写锁:写操作-加锁: %@",key);
[dic setObject:obj forKey:key];
sleep(2);
pthread_rwlock_unlock(&lock); //解锁
NSLog(@"读写锁:写操作-解锁: %@",key);
}
@end
- 异步并行调用:
dispatch_queue_t queue = dispatch_queue_create("test_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[self setObject:@"测试1" forKey:@"key1"];
[self objectForKey:@"key1"];
});
dispatch_async(queue, ^{
[self setObject:@"测试2" forKey:@"key2"];
[self objectForKey:@"key2"];
});
dispatch_async(queue, ^{
[self setObject:@"测试3" forKey:@"key3"];
[self objectForKey:@"key3"];
});
- 输出结果:
// 写 - 排他性
2021-12-31 15:30:32.041620+0800 读写锁:写操作-加锁: key1
2021-12-31 15:30:34.047163+0800 读写锁:写操作-解锁: key1
2021-12-31 15:30:34.047252+0800 读写锁:写操作-加锁: key2
2021-12-31 15:30:36.051089+0800 读写锁:写操作-解锁: key2
2021-12-31 15:30:36.051185+0800 读写锁:写操作-加锁: key3
2021-12-31 15:30:38.056850+0800 读写锁:写操作-解锁: key3
// 读 - 并发
2021-12-31 15:30:38.056931+0800 读写锁:读操作-加锁: key1
2021-12-31 15:30:38.056944+0800 读写锁:读操作-加锁: key2
2021-12-31 15:30:38.057284+0800 读写锁:读操作-加锁: key3
2021-12-31 15:30:40.062497+0800 读写锁:读操作-解锁: key2
2021-12-31 15:30:40.062497+0800 读写锁:读操作-解锁: key1
2021-12-31 15:30:40.062643+0800 读写锁:读操作-解锁: key3
- 结论:
写锁`互斥`,读锁可`并行`;
读写锁:
- 是一种特殊的自旋锁;
- 读写锁对于自旋锁而言,能`提高并发性`,在多核系统中,允许`多个读者`来访问共享资源;
- 写者是排他性的,一个读写锁同时`只能有一个写者`或`多个读者`,但不能同时既有写者又有写者;
- 如果读写锁当前`没有读者,也没有写者`,那么`写者`可以`立即获得`读写锁,
否则它必须`自旋`在那里,直到`没有任何的写者或读者`;
即在`读加锁`状态时,所有以`读模式`对它加锁的线程都`可以获得访问权`;
当有写模式试图加锁时,读写锁通常会`阻塞随后的读模式锁请求`,防止读模式锁长期被占用,而写模式锁长期被阻塞;
- 如果读写锁`没有写者`,那么`读者`可以`立即获得`读写锁,
否则读者必须`自旋`在那里,知道`写者释放读写锁`;
即以`写模式`进行加锁,必须等`所有线程释放锁`;
网友评论