美文网首页
关于iOS使用gcd barrier函数实现多读单写的思考

关于iOS使用gcd barrier函数实现多读单写的思考

作者: 神秘唤猫人 | 来源:发表于2019-07-16 13:55 被阅读0次

看简书面试题时看到这样一篇gcd相关的文章

提到多读单写,并有配图如下

image

- (id)readDataForKey:(NSString*)key

{

    __block id result;

    dispatch_sync(_concurrentQueue, ^{

        result = [self valueForKey:key];

    });

    returnresult;

}

- (void)writeData:(id)data forKey:(NSString*)key

{

    dispatch_barrier_async(_concurrentQueue, ^{

        [self setValue:data forKey:key];

    });

}

单写没有问题,
多读?需要在任意队列异步执行readDataForKey方法,_concurrentQueue虽然能并发执行任务, 但是如果添加任务方法被同步执行, 任务执行完才能添加下一个任务, 等同于单写

相关文章

网友评论

      本文标题:关于iOS使用gcd barrier函数实现多读单写的思考

      本文链接:https://www.haomeiwen.com/subject/aqazkctx.html