美文网首页
iOS-多线程-barrier

iOS-多线程-barrier

作者: 笑破天 | 来源:发表于2022-06-27 23:29 被阅读0次

    dispatch_barrier_(a)sync
    注意:如果传入的是一个全局并发队列或串行队列,dispatch_barrier_sync 就相当于 dispatch_async

    dispatch_queue_t queue = dispatch_queue_create("thread", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSLog(@"test1");
        });
        dispatch_async(queue, ^{
            NSLog(@"test2");
        });
        dispatch_async(queue, ^{
            NSLog(@"test3");
        });
        
        dispatch_barrier_sync(queue, ^{
            for (int i = 0; i < 500000000; i++) {
                if (5000 == i) {
                    NSLog(@"point1");
                } else if (500000 == i) {
                    NSLog(@"point2");
                } else if (50000000 == i) {
                    NSLog(@"point3");
                }
            }
            NSLog(@"barrier");
        });
        NSLog(@"aaa");
        dispatch_async(queue, ^{
            NSLog(@"test4");
        });
        NSLog(@"bbb");
        dispatch_async(queue, ^{
            NSLog(@"test5");
        });
        dispatch_async(queue, ^{
            NSLog(@"test6");
        });
    

    https://blog.csdn.net/willluckysmile/article/details/61195571
    dispatch_barrier负责对该队列中的任务进行前后分割,sync/async负责是否阻塞位于其之后的非该队列的任务。一般用sync比较符合直观逻辑。

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
        dispatch_sync(queue, ^{
            NSLog(@"%zu", 10);
        });
        NSLog(@"done");
    // 输出:10 done
    

    相关文章

      网友评论

          本文标题:iOS-多线程-barrier

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