1.读写锁(pthread_rwlock_t)

#import <pthread.h>
@interface TKReadWhiteSafeDic() {
    // 声明一个读写锁
    pthread_rwlock_t  lock;
    // 定义一个并发队列
    dispatch_queue_t concurrent_queue;
    // 用户数据中心, 可能多个线程需要数据访问
    NSMutableDictionary *userCenterDic;
}

@end

// 多读单写模型
@implementation TKReadWhiteSafeDic

- (id)init {
    self = [super init];
    if (self) {
        //初始化读写锁
        pthread_rwlock_init(&lock,NULL);
        // 创建数据容器
        userCenterDic = [NSMutableDictionary dictionary];
        // 通过宏定义 DISPATCH_QUEUE_CONCURRENT 创建一个并发队列
        concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
    }
    return self;
}

- (id)objectForKey:(NSString *)key {
    //加读锁
    pthread_rwlock_rdlock(&_rwlock);
    id obj = [userCenterDic objectForKey:key];
    pthread_rwlock_unlock(&_rwlock);
    return obj;
}

- (void)setObject:(id)obj forKey:(NSString *)key {
     //加写锁
    pthread_rwlock_wrlock(&_rwlock);
    [userCenterDic setObject:obj forKey:key];
    pthread_rwlock_unlock(&_rwlock);    
}

2.dispatch_barrie

@interface TKReadWhiteSafeDic() {
    // 定义一个并发队列
    dispatch_queue_t concurrent_queue;
    
    // 用户数据中心, 可能多个线程需要数据访问
    NSMutableDictionary *userCenterDic;
}

@end

// 多读单写模型
@implementation TKReadWhiteSafeDic

- (id)init {
    self = [super init];
    if (self) {
        // 通过宏定义 DISPATCH_QUEUE_CONCURRENT 创建一个并发队列
        concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
        // 创建数据容器
        userCenterDic = [NSMutableDictionary dictionary];
    }
    return self;
}

- (id)objectForKey:(NSString *)key {
    __block id obj;
    // 同步读取指定数据
    dispatch_sync(concurrent_queue, ^{
        obj = [userCenterDic objectForKey:key];
    });
    return obj;
}

- (void)setObject:(id)obj forKey:(NSString *)key {
    // 异步栅栏调用设置数据
    dispatch_barrier_async(concurrent_queue, ^{
        [userCenterDic setObject:obj forKey:key];
    });
}