OC实现读写锁的两种方法

2024-04-14  本文已影响0人  扫地僧练级
读写锁是一种特殊的自旋锁;
读写锁对于自旋锁而言,能`提高并发性`,在多核系统中,允许`多个读者`来访问共享资源;
写者是排他性的,一个读写锁同时`只能有一个写者`或`多个读者`,但不能同时既有写者又有写者;

解释:
- 如果读写锁当前`没有读者,也没有写者`,那么`写者`可以`立即获得`读写锁,
  否则它必须`自旋`在那里,直到`没有任何的写者或读者`;
  即在`读加锁`状态时,所有以`读模式`对它加锁的线程都`可以获得访问权`;
  当有写模式试图加锁时,读写锁通常会`阻塞随后的读模式锁请求`,防止读模式锁长期被占用,而写模式锁长期被阻塞;

- 如果读写锁`没有写者`,那么`读者`可以`立即获得`读写锁,
  否则读者必须`自旋`在那里,知道`写者释放读写锁`;
  即以`写模式`进行加锁,必须等`所有线程释放锁`;

实现读写锁的两种方式:
一: GCD

@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;
}

@end

二:pthread_rwlock_t

#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
上一篇下一篇

猜你喜欢

热点阅读