iOS 多读单写,栅栏函数` dispatch_baarrier_async 和dispatch_baarrier_sync 区别`

iOS 多读单写 以及栅栏函数讲解dispatch_barrier_async

dispatch_barrier_(a)sync 栅栏函数不能使用在全部并发队列中或者同步队列中,否则会失去他的意义。

文档中解释:

dispatch_barrier_sync: Submits a barrier block object for execution and waits until that block completes.

dispatch_barrier_async: Submits a barrier block for asynchronous execution and returns immediately.

sync:提交一个栅栏函数,他会等待栅栏函数执行完毕
async:无需等待立刻返回

怎样实现多读单写?

比如在内存中维护一份数据,有多处地方可能会同时操作这块数据,怎么能保证数据安全?

1.读写互斥

2.写写互斥

3.读读并发

要想满足上面的三点该怎么实现呢?

- (void)viewDidLoad {
    [super viewDidLoad];

    _queue = dispatch_queue_create("com.rongcloud.sunchengxiu", DISPATCH_QUEUE_CONCURRENT);
    _dic = [NSMutableDictionary dictionary];
    [self testBarrier];
}
- (void)testBarrier{
    dispatch_queue_t queue = dispatch_queue_create("com.rongcloud.barrier", DISPATCH_QUEUE_CONCURRENT);
    for (int i = 0 ; i < 10; i ++) {
        dispatch_async(queue, ^{
            [self setObject:@(i) key:[NSString stringWithFormat:@"%@",@(i)]];
        });
       
    }
    sleep(3);
    // 如果将队列换成并发队列呢?
    dispatch_queue_t q = dispatch_queue_create("qweqw", DISPATCH_QUEUE_CONCURRENT);
    for (int i = 0 ; i < 10; i ++) {
        // 如果换成同步呢?各种搭配呢?
        dispatch_async(q, ^{
            NSLog(@"%@",[self objectForKey:[NSString stringWithFormat:@"%@",@(i)]]);
        });
    }
}
- (id)objectForKey:(NSString *)key{
    __block id obj;
    // 同步读取数据
    dispatch_sync(_queue, ^{
        obj = [_dic objectForKey:key];
    });
    return obj;
}
- (void)setObject:(id)object key:(NSString *)key{
    // 异步栅栏设置数据
    dispatch_barrier_async(_queue, ^{
        NSLog(@"set:%@",key);
        [_dic setObject:object forKey:key];
    });
}

objectForKey

objectForKey 是再 _queue 这个并发队列中执行的,所以这个方法就可以在多个线程队列同时调用的,因为我的读操作需要同步返回的,所以治理使用sync。可以看下打印的结果,如果将

for (int i = 0 ; i < 10; i ++) {
        dispatch_async(q, ^{
            NSLog(@"%@",[self objectForKey:[NSString stringWithFormat:@"%@",@(i)]]);
        });
    }

换成


// 如果将队列换成串行队列呢?
    dispatch_queue_t q = dispatch_queue_create("qweqw", NULL);
    for (int i = 0 ; i < 10; i ++) {
        // 如果换成同步呢?各种搭配呢?
        dispatch_async(q, ^{
            NSLog(@"%@",[NSString stringWithFormat:@"%@",@(i)]);
        });
    }

上面这样,以及各种搭配,打印结果又是什么呢?原理是什么呢?看过我之前的文章应该就明白了。这里就关于一下同步并发队列同步穿行队列,异步并发队列等的一些理解了。

setObject

这里使用了栅栏函数,并且使用了async,上面写道无需等到,会立刻返回,但是栅栏函数会等待之前操作完成之后才会继续执行,这样写操作就是相互栅栏函数,就形成了互斥,

dispatch_barrier_async 和dispatch_barrier_sync

- (void)viewDidLoad {
    [super viewDidLoad];

    _queue = dispatch_queue_create("com.rongcloud.sunchengxiu", DISPATCH_QUEUE_CONCURRENT);
    _dic = [NSMutableDictionary dictionary];
    [self barrier];
}
- (void)barrier{
    NSLog(@"start");
    dispatch_async(_queue, ^{
        NSLog(@"1");
    });
    dispatch_async(_queue, ^{
        NSLog(@"2");
    });
    dispatch_barrier_async(_queue, ^{
        NSLog(@"3");
    });
    NSLog(@"6");
    dispatch_async(_queue, ^{
        NSLog(@"4");
    });
    dispatch_async(_queue, ^{
        NSLog(@"5");
    });
     NSLog(@"end");
}

上面的打印结果大概是遮掩改的


2019-12-17 22:04:36.882818+0800 blogTest[40717:3155160] start
2019-12-17 22:04:36.883029+0800 blogTest[40717:3155160] 6
2019-12-17 22:04:36.883052+0800 blogTest[40717:3155203] 1
2019-12-17 22:04:36.883073+0800 blogTest[40717:3155207] 2
2019-12-17 22:04:36.883168+0800 blogTest[40717:3155160] end
2019-12-17 22:04:36.883237+0800 blogTest[40717:3155207] 3
2019-12-17 22:04:36.883374+0800 blogTest[40717:3155207] 4
2019-12-17 22:04:36.883374+0800 blogTest[40717:3155213] 5


如果换成这样

- (void)barrier{
    NSLog(@"start");
    dispatch_async(_queue, ^{
        sleep(3);
        NSLog(@"1");
    });
    dispatch_async(_queue, ^{
        NSLog(@"2");
    });
    dispatch_sync(_queue, ^{
        sleep(1);
        NSLog(@"7");
    });
    dispatch_barrier_async(_queue, ^{
        NSLog(@"3");
    });
    NSLog(@"6");
    dispatch_async(_queue, ^{
        NSLog(@"4");
    });
    dispatch_async(_queue, ^{
        NSLog(@"5");
    });
     NSLog(@"end");
}

打印为

2019-12-17 22:08:54.803436+0800 blogTest[40766:3159325] start
2019-12-17 22:08:54.803692+0800 blogTest[40766:3159391] 2
2019-12-17 22:08:55.803769+0800 blogTest[40766:3159325] 7
2019-12-17 22:08:55.804128+0800 blogTest[40766:3159325] 6
2019-12-17 22:08:55.804519+0800 blogTest[40766:3159325] end
2019-12-17 22:08:57.804597+0800 blogTest[40766:3159408] 1
2019-12-17 22:08:57.804961+0800 blogTest[40766:3159408] 3
2019-12-17 22:08:57.805246+0800 blogTest[40766:3159408] 4
2019-12-17 22:08:57.805252+0800 blogTest[40766:3159389] 5


说明 dispatch_barrier_async , 会将添加到queue前面的任务执行完之后,才会执行后面的任务,并不会阻塞当前的线程,45一定在3后
如果改成:

- (void)barrier{
    NSLog(@"start");
    dispatch_async(_queue, ^{
        sleep(3);
        NSLog(@"1");
    });
    dispatch_async(_queue, ^{
        NSLog(@"2");
    });
    dispatch_sync(_queue, ^{
        sleep(1);
        NSLog(@"7");
    });
    dispatch_barrier_sync(_queue, ^{
        NSLog(@"3");
    });
    NSLog(@"6");
    dispatch_async(_queue, ^{
        NSLog(@"4");
    });
    dispatch_async(_queue, ^{
        NSLog(@"5");
    });
     NSLog(@"end");
}

打印结果为


2019-12-17 22:12:02.286585+0800 blogTest[40788:3162528] start
2019-12-17 22:12:02.286808+0800 blogTest[40788:3162606] 2
2019-12-17 22:12:03.287985+0800 blogTest[40788:3162528] 7
2019-12-17 22:12:05.289600+0800 blogTest[40788:3162607] 1
2019-12-17 22:12:05.289959+0800 blogTest[40788:3162528] 3
2019-12-17 22:12:05.290163+0800 blogTest[40788:3162528] 6
2019-12-17 22:12:05.290468+0800 blogTest[40788:3162528] end
2019-12-17 22:12:05.290484+0800 blogTest[40788:3162607] 4
2019-12-17 22:12:05.290501+0800 blogTest[40788:3162608] 5

一样的,后被添加到栅栏函数的queue中的任务,一需要等待栅栏函数前面的任务执行完毕之后,才可以执行,也就是说127一定在3前面,45一定在3后,而不一样的是6和end的打印,当换成sync之后会阻塞当前线程会等待前面都执行完毕之后,才会打印这两个,而之前的async,是不会阻塞的,会打印这两个值,而其余的任务有顺序,而换成sync之后,在当前线程执行的任务也有了顺序,这就是dispatch_barrier_sync 和 dispatch_barrier_async 的区别

你可能感兴趣的:(iOS 多读单写,栅栏函数` dispatch_baarrier_async 和dispatch_baarrier_sync 区别`)